1 Star 0 Fork 1K

林振书/微信电商小程序

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.js 20.51 KB
一键复制 编辑 原始数据 按行查看 历史
daqiang 提交于 2018-03-16 15:23 . 新增转发统一接口
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
//app.js
import { clientInterface } from "/public/clientInterface.js";
import { dellUrl } from "/public/requestUrl.js";
App({
clientUrl: 'https://mini.sansancloud.com/chainalliance/', // 链接地址
// clientUrl:'https://mini.sansancloud.com/chainalliance/', // 链接地址
/**
* 切换项目的开关 ↓↓↓↓↓
*/
clientNo:'xianhua', //自定义的项目的名称。
clientName:'',
more_scene:'', //扫码进入场景 用来分销
shareParam:null,//分享页面参数
miniIndexPage:'',
setting : null,
loginUser: null,
cookie:null,
shopOpen:null, // 店铺营业时间-开关
cart_offline:[],
//addr:null,
payItem:null, //下单的时候传过去的
userSign:null, //账号密码
EditAddr:null,//传值的
richTextHtml:'',
productParam:null,//传值的
// customPageJson:null,//page的动态组件json
header: {
'content-type': 'application/json' // 默认值
},
headerPost: {
'content-type': 'application/x-www-form-urlencoded'
},
successOnlaunch:false,
/* 页面影藏 */
appHide:false,
onHide:function(e){
console.log('hide')
console.log(e)
this.appHide = true
},
onShow:function(e){
console.log('show')
console.log(e)
/* let pagePath = e.path
if(this.appHide){
this.appHide = false
} */
},
onLaunchOptions:{},
/* 第一次加载 */
onLaunch: function (options) {
this.onLaunchOptions = options
let that = this
console.log('------onlauch------')
console.log(options)
/* 第三方配置加载 clientNo */
/*
wx.getExtConfig({
success: function (res) {
console.log('第三方配置')
console.log(res)
if (res.extConfig && res.extConfig.clientNo) {
console.log(res.extConfig)
that.clientNo = res.extConfig.clientNo
}
that.clientNo = 'xianhua'
},
complete: function (res) {
}
})*/
let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
if (extConfig.clientNo) {
that.clientNo = res.extConfig.clientNo
}
console.log('==='+this.clientNo+'====')
console.log(wx.getExtConfigSync())
let inputPlatformNo = options.query.platformNo;
if (!!inputPlatformNo) {
this.clientNo = inputPlatformNo
}
let more_scene = decodeURIComponent(options.query.scene)
console.log(more_scene)
if (more_scene){
this.more_scene = more_scene
}
that.loadFirstEnter(more_scene)
},
timer: 0,
// 确保onLaunch事件完成后再开始调用其他函数
promiseonLaunch:function(self){
let that = this
console.log('promiseonLaunch')
if (!!this.setting) {
self.onLoad()
}
else {
that.timer = setTimeout(function () {
that.promiseonLaunch(self);
}, 500);
}
},
//第一次登录加载的函数
loadFirstEnter: function (more_scene){
this.getSetting()
this.wxLogin(more_scene)
},
loadScene: function (inputPlatformNo){
this.clientNo = inputPlatformNo
},
globalData: {
userInfo: null,
sansanUser:null,
sysWidth: wx.getSystemInfoSync().windowWidth, //图片宽度
sysHeight: wx.getSystemInfoSync().windowHeight,
},
toIndex:function(){
console.log('首页叫做:'+this.miniIndexPage)
//这个需要注意 switchTab 和 redirectTo
if (this.miniIndexPage){
wx.switchTab({
url: '/pages/' + this.miniIndexPage+'/index',
})
}else{
wx.switchTab({
url: '/pages/custom_page_index/index',
})
}
},
//加载失败处理
loadFail:function(){
let that = this
/*
wx.showModal({
title: '提示',
content: '加载失败,重新加载',
success: function (res) {
if (res.confirm) {
that.toIndex()
} else if (res.cancel) {
}
}
})*/
wx.showToast({
title: "加载失败",
image: '/images/icons/tip.png',
duration: 2000
})
},
//检查是否已经登录
checkIfLogin:function(){
if (this.loginUser){
console.log('已经登录了')
return true
} else{
console.log('未登录')
wx.showModal({
title: '提示',
content: '用户未登录',
success: function (res) {
if (res.confirm) {
wx.navigateTo({
url: '/pages/login/index'
})
} else if (res.cancel) {
}
}
})
return false
}
},
//检查商家开店?
checkShopOpenTime:function(){
let that = this
let shopBean = this.setting.platformSetting.defaultShopBean
let nowTime = {
hour:'',
minutes:''
}
let myDate = new Date();
nowTime.hour = myDate.getHours()
nowTime.minutes = myDate.getMinutes()
let myTime = ''
if (nowTime.minutes < 10){
myTime = Number(nowTime.hour + '.0' + nowTime.minutes)
}else{
myTime = Number(nowTime.hour + '.' + nowTime.minutes)
}
if (myTime < Number(shopBean.serviceStartTime) || myTime > Number(shopBean.serviceEndTime) ){
wx.showModal({
title: '不是营业时间',
content: '营业时间为' + shopBean.serviceStartTime + '-' + shopBean.serviceEndTime,
success: function (res) {
if (res.confirm) {
that.toIndex()
} else if (res.cancel) {
}
}
})
return false
}else{
return true
}
},
/* 处理url的函数,放到app里吧 */
AddClientUrl: function (url, params, method, random){
let loginToken = ''
if (!this.loginUser || !this.loginUser.platformUser || !this.loginUser.platformUser.loginToken){
loginToken = ''
}else{
loginToken = this.loginUser.platformUser.loginToken
}
var returnUrl = dellUrl(url, params, method, random, loginToken )
returnUrl.url = this.clientUrl + this.clientNo + returnUrl.url
return returnUrl;
},
/* 解析LinkUrl */
getUrlParams: function (url) {
console.log('------getUrlParams--------')
console.log(url)
let theResult = {
url: '',
param: ''
}
if (url.indexOf('?') != -1) {
let str2 = url.substr(0, url.indexOf('?')-5);
let str3 = url.substr(url.indexOf('?'));
theResult.url = str2
theResult.param = str3
}
if (url.indexOf('?') == -1){
let str2 = url.substr(0, url.indexOf('.'))
let str3 = ''
theResult.url = str2
theResult.param = str3
}
return theResult
},
getSpaceStr: function (str,p) {
let theResult = {
str1: '',
str2: ''
}
if (str.indexOf(p) != -1) {
let str2 = str.substr(0, str.indexOf(p));
let str3 = str.substr(str.indexOf(p)+1);
theResult.str1 = str2
theResult.str2 = str3
}
return theResult
},
/* 转换成str 带?*/
jsonToStr: function (json) {
var returnParam = "?"
var str = [];
for (var p in json) {
str.push(p + "=" + json[p]);
//str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
}
returnParam += str.join("&")
console.log(returnParam)
return returnParam
},
// 不带问号 过滤loginTocken
jsonToStr2: function (json) {
var returnParam = ""
var str = [];
for (var p in json) {
if(p != 'loginToken'){
str.push(p + "=" + json[p]);
}
}
returnParam += str.join("&")
console.log(returnParam)
return returnParam
},
//link事件 绑定导向对应的控件上
linkEvent: function (linkUrl) {
if (!linkUrl){
return
}
let urlData = this.getUrlParams(linkUrl)
let If_Order_url = urlData.url.substr(0, 10)
console.log('-----toGridLinkUrl---------')
console.log(urlData)
console.log(If_Order_url)
if (linkUrl.substr(0, 3) == 'tel') {
wx.navigateTo({
url: '/pages/custom_page_contact/index',
})
}
else if (linkUrl.substr(0, 12) == 'custom_page_') {
var resultUrl = linkUrl.substring(12, linkUrl.length - 5)
if (urlData.param == '') {
urlData.param = '?'
}
wx.navigateTo({
url: '/pages/custom_page/index' + urlData.param + '&Cpage=' + resultUrl,
})
}
else if (If_Order_url == 'order_list') {
wx.navigateTo({
url: '/pages/' + 'order_list_tab' + '/index' + urlData.param,
})
}
else if (linkUrl.substr(0, 15) == 'product_detail_') {
let productId = linkUrl.replace(/[^0-9]/ig, "");
wx.navigateTo({
url: '/pages/productDetail/index?id=' + productId + "&addShopId=236",
})
}
else if (urlData.url == 'shop_map'){
this.openLocation()
}
else {
wx.navigateTo({
url: "/pages/" + urlData.url + "/index" + urlData.param,
})
}
},
checkLogin:function(){
//let that = this
if (!this.loginUser){
this.wxLogin()
}
},
/* 检查是否过期 */
checkSession:function(){
let that = this
wx.checkSession({
success: function () {
console.log('session 未过期,并且在本生命周期一直有效')
wx.getStorage({
//拿cookie
key: 'cookie',
success: function (res) {
that.cookie = res.data
that.header = {
'content-type': 'application/json', // 默认值
'Cookie': res.data
}
that.headerPost = {
'content-type': 'application/x-www-form-urlencoded',
'Cookie': res.data
}
}
})
wx.getStorage({
key: 'loginUser',
success: function (res) {
that.loginUser = res.data
}
})
},
fail: function () {
//登录态过期
console.log('登录态过期')
that.wxLogin()
}
})
},
/* 设置cookie */
setCookie:function(cookie){
this.cookie = cookie
this.header = {
'content-type': 'application/json', // 默认值
'Cookie': cookie
}
this.headerPost = {
'content-type': 'application/x-www-form-urlencoded',
'Cookie': cookie
}
},
//存用户信息
setloginUser: function (loginUser,cookie){
//console.log('--------setloginUser----------')
//console.log(loginUser)
//console.log(cookie)
if (loginUser){
wx.setStorage({
key: "loginUser",
data: loginUser
})
}
if (cookie){
wx.setStorage({
key: "cookie",
data: cookie
})
}
},
//获取已经登录了的用户信息和login时一样
get_session_userinfo: function () {
let customIndex = this.AddClientUrl("/get_session_userinfo.html", {}, 'post')
let that = this
wx.request({
url: customIndex.url, //仅为示例,并非真实的接口地址
data: customIndex.params,
header: that.headerPost,
success: function (res) {
console.log('===========get_session_userinfo============')
if (res.data.errcode == 0) {
console.log(res.data.relateObj)
that.loginUser = res.data.relateObj
that.setloginUser(res.data.relateObj)
}
},
fail: function (res) {
}
})
},
//sentWxUserInfo 第一次登录给他设置头像
sentWxUserInfo:function(){
let that = this
let userInfo = this.globalData.userInfo
wx.getUserInfo({
withCredentials: false,
success: function (res) {
userInfo = res.userInfo
let infoParam = {
headimg: '',
nickname: '',
sex: ''
}
infoParam.headimg = userInfo.avatarUrl
infoParam.nickname = userInfo.nickName
infoParam.sex = userInfo.gender
let customIndex = that.AddClientUrl("/change_user_info.html", infoParam, 'post')
wx.request({
url: customIndex.url,
data: customIndex.params,
header: that.headerPost,
method: 'POST',
success: function (res) {
console.log('---change_user_info----- success-')
console.log(res.data)
if (res.errcode == 0) {
{
that.loginUser.nickName = userInfo.nickName;
that.loginUser.sex = userInfo.sex;
that.loginUser.userIcon = userInfo.avatarUrl;
}
console.log('-----第一次登录 传头像成功 --------')
//that.wxLogin()
that.get_session_userinfo()
} else {
console.log('-----第一次登录 传头像失败 --------')
that.get_session_userinfo()
}
},
fail: function (res) {
console.log('-----第一次登录 传头像失败 回调fail--------')
console.log()
},
complete: function (res) {
},
})
}
})
},
/* 微信登录测试 */
wxLogin: function (more_scene) {
if (!more_scene || more_scene == 'undefined'){
more_scene = '0'
}
console.log('--------------微信登录--------------')
wx.showLoading({
title: '登录中',
mask: true
})
var that = this
wx.login({
success: function (res) {
console.log(res.code)
if (res.code && res.code.indexOf('mock') == -1) {
console.log('---------------------')
console.log(res)
console.log('---------------------')
//发起网络请求
let loginParam = {}
loginParam.code = res.code
loginParam.scene = more_scene
let customIndex = that.AddClientUrl("/wx_mini_code_login.html", loginParam, 'post')
wx.request({
url: customIndex.url,
data: customIndex.params,
header: that.headerPost,
method: 'POST',
success: function (e) {
console.log('----22222 success------')
console.log(e)
if (e.data.errcode == 0) {
let header = e.header
let cookie = null
if (!!header['Set-Cookie']) {
cookie = header['Set-Cookie']
}
if (!!header['set-cookie']) {
cookie = header['set-cookie']
}
let loginJson = e.data.relateObj
that.setCookie(cookie)
that.setloginUser(e.data.relateObj, cookie)
console.log('登陆成功')
that.loginUser = e.data.relateObj
that.globalData.sansanUser = e.data.relateObj
wx.hideLoading()
that.get_session_userinfo()
if (!loginJson.nickName || loginJson.nickName == loginJson.name) {
that.sentWxUserInfo()
}
// that.toIndex()
}else{
wx.hideLoading()
/* wx.showModal({
title: '提示',
content: '登录失败,重新登录',
success: function (res) {
if (res.confirm) {
wx.navigateTo({
url: '/pages/login/index'
})
} else if (res.cancel) {
}
}
}) */
wx.showToast({
title: '登录失败',
image: '/images/icons/tip.png',
duration: 2000
})
}
},
fail: function (e) {
console.log('----fail------')
console.log(e)
/* wx.showModal({
title: '提示',
content: '登录失败,重新登录',
success: function (res) {
if (res.confirm) {
wx.navigateTo({
url: '/pages/login/index'
})
} else if (res.cancel) {
}
}
}) */
wx.showToast({
title: '登录失败',
image: '/images/icons/tip.png',
duration: 2000
})
}
})
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
},
fail: function (res) {
console.log('---------111111 fail----------')
console.log(res)
},
complete: function (res) {
console.log('---------111111 complete----------')
console.log(res)
wx.hideLoading()
},
});
},
//获取setting
getSetting: function (self) {
if(!self){
self = 0
}
console.log('**************************************')
var settUrl = this.AddClientUrl("/get_platform_setting.html")
var that = this
//拿setting
wx.request({
url: settUrl.url, //仅为示例,并非真实的接口地址
header: that.header,
success: function (res) {
console.log(res.data)
if (res.data.platformSetting) {
that.clientName = res.data.platformSetting.platformName
let categories = res.data.platformSetting.categories
let allType = {}
allType.id = 'all'
allType.name = '全部'
allType.active = true
for (let i = 0; i < categories.length; i++) {
categories[i].active = false
}
categories.unshift(allType)
}
that.setting = res.data
if (res.data.platformSetting.miniIndexPage){
let miniIndexPage = that.getSpaceStr(res.data.platformSetting.miniIndexPage, '.')
that.miniIndexPage = miniIndexPage.str1
}else{
that.miniIndexPage = 'custom_page_index'
}
if (!self){
}else{
self.setData({ setting: res.data })
self.setNavBar()
}
wx.hideLoading()
return
let ShopBean = res.data.platformSetting.defaultShopBean
if (ShopBean.serviceStartTime ){
}
// 完成初次加载
that.successOnlaunch = true
},
fail: function (res) {
wx.hideLoading()
that.loadFail()
}
})
},
//微信内部地图
openLocation: function () {
console.log('---------打开地图-------')
let markers = this.setting.platformSetting.defaultShopBean
let lat = Number(markers.latitude)
let lng = Number(markers.longitude)
let name = markers.shopName
let address = ''
wx.getLocation({
type: 'wgs84', //返回可以用于wx.openLocation的经纬度
success: function (res) {
console.log('11111')
wx.openLocation({
latitude: Number(markers.latitude),
longitude: Number(markers.longitude),
scale: 28,
name: name,
address: address,
success: function (res) {
console.log(res)
},
fail: function (res) {
console.log(res)
}
})
},fail:function(res){
console.log('22222')
console.log(res)
}
})
},
//带参转发
shareForFx: function (pageName, pageTitle, pageCode) {
let that = this
let AllCode = ''
let fxCode = '' //userId
if (this.loginUser){
fxCode = 'scene=MINI_PLATFORM_USER_ID_' + this.loginUser.platformUser.id
}else{
fxCode = 'scene=MINI_PLATFORM_USER_ID_' + this.more_scene
}
if (!pageName) {
pageName = 'index'
}
if (!pageTitle) {
pageTitle = that.clientName
}
if (!pageCode) {
pageCode = ''
AllCode = fxCode
} else {
AllCode = fxCode + '&' + pageCode
}
return {
title: pageTitle,
path: '/pages/' + pageName + '/index?' + AllCode,
success: function (res) {
},
fail: function (res) {
}
}
},
shareForFx2: function (pageName, pageTitle, pageCode, imageUrl) {
//组合参数,交给custompage_index 解析
// 组合参数所带
let that = this
let AllCode = ''
let fxCode = '' //userId
if (this.loginUser) {
fxCode = 'scene=MINI_PLATFORM_USER_ID_' + this.loginUser.platformUser.id
}
if (!pageName) {
pageName = 'custom_page_index'
}
if (!pageTitle) {
pageTitle = that.clientName
}
if (!pageCode) {
pageCode = {}
}
pageCode.scene = 'MINI_PLATFORM_USER_ID_' + this.loginUser.platformUser.id
pageCode.pageName = pageName
AllCode = that.jsonToStr2(pageCode)
console.log('转发出去的参数集合: '+AllCode)
return {
title: pageTitle,
path: '/pages/index/index?' + AllCode,
imageUrl:imageUrl,
success: function (res) {
},
fail: function (res) {
}
}
},
})
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
微信
1
https://gitee.com/lzs0223/wechatApp.git
git@gitee.com:lzs0223/wechatApp.git
lzs0223
wechatApp
微信电商小程序
master

搜索帮助