app.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // app.js
  2. /**
  3. * 新版备注:
  4. * 1. 两个流程 fare_type 0外卖 1自提 默认自提
  5. * 2. 有会员价
  6. * 3. 商品分规格, 有规格商品和无规格商品同时存在
  7. * 4. 获取应用实例 一律直接用getApp()
  8. * 5. 无主动退出
  9. * 6. 用户右上角的分享,只开首页和个人中心,其他页面都关闭
  10. * 7. 如需在接口成功时弹出返回信息,请单独加一下toast,避免与微信的loading冲突(wx.showLoading 与wx.showToast不能同时存在)
  11. * 8. 如需当接口连续调用,共用一个loading时,请传参nextInvoke,且主动控制何时关闭loading
  12. *
  13. * **/
  14. let myPro = require("/utils/wxRequest.js");
  15. App({
  16. onLaunch() {
  17. let that = this;
  18. // 拿code换openid(先放onLaunch试一段)
  19. wx.login({
  20. success: function(res) {
  21. // console.log('获取wxcode',res)
  22. let params = {
  23. code: res.code
  24. };
  25. myPro.wxRequest("user/mini-auth","POST",params).then(res=>{
  26. let result = res.result;
  27. // 有token 是老用户
  28. if(result.token){
  29. that.globalData.token = result.token;
  30. wx.setStorage({
  31. key: "token",
  32. data: result.token
  33. });
  34. that.globalData.is_member = result.user.is_member; // 会员 0否 1是
  35. that.globalData.userInfo = {
  36. nickname: result.user.nickname,
  37. thumb: result.user.thumb,
  38. phone: result.user.phone
  39. };
  40. if(result.user.store){
  41. that.globalData.role = 1; // 有店铺,即为门店
  42. };
  43. // console.log('用户',that.globalData.userInfo)
  44. // 拿用户信息(供首页使用)
  45. }else{
  46. // 新用户
  47. that.globalData.openid = result.openid;
  48. that.globalData.session_key = result.session_key;
  49. }
  50. }).catch(err=>{
  51. console.log('报错信息',err);
  52. wx.showToast({
  53. title: err,
  54. icon: "none"
  55. })
  56. })
  57. },
  58. fail: function(res){
  59. wx.showToast({
  60. title: "微信未授权成功,请在设置中允许使用用户信息",
  61. icon: "none"
  62. });
  63. }
  64. });
  65. },
  66. onShow(){
  67. // console.log('onShow');
  68. let that = this;
  69. // 设备信息
  70. wx.getSystemInfo({
  71. success(res) {
  72. // 获取系统信息
  73. const systemInfo = wx.getSystemInfoSync();
  74. // 胶囊按钮位置信息
  75. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  76. // 胶囊位置(初始位置)
  77. that.globalData.customTop = menuButtonInfo.top;
  78. // 胶囊位置(中间位置)
  79. that.globalData.customCenter =
  80. menuButtonInfo.top + menuButtonInfo.height / 2;
  81. // 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度
  82. that.globalData.setBgHeight =
  83. (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 +
  84. menuButtonInfo.height +
  85. systemInfo.statusBarHeight;
  86. },
  87. });
  88. // 定位
  89. // that.getLocal();
  90. },
  91. // 验证登录
  92. verifyLogin(callBack){
  93. const that = this;
  94. if(!callBack){
  95. callBack = function(){};
  96. };
  97. if(!that.globalData.token){
  98. let pages = getCurrentPages();
  99. if(pages.length > 0){
  100. wx.setStorage({
  101. key: 'loginCallBackPage',
  102. data: that.pageBackUrl(),
  103. success: function(){
  104. wx.reLaunch({
  105. url: '/pages/login/index/index'
  106. })
  107. },
  108. })
  109. };
  110. callBack(false);
  111. }else{
  112. callBack(true);
  113. }
  114. },
  115. // 回跳地址
  116. pageBackUrl: function(){
  117. let pages = getCurrentPages();
  118. let currentPage = pages[pages.length -1];
  119. let url= currentPage.route;
  120. let options = JSON.stringify(currentPage.options);
  121. options = options.replace(/[\{\}\"]/g,'');
  122. options = options.replace(/\:/g,'=');
  123. options = options.replace(/\,/g,'&');
  124. url = url + '?' + options;
  125. // console.log('回跳地址',url)
  126. return url
  127. },
  128. // 定位授权
  129. getLocal: function (callback) {
  130. var that = this;
  131. if (callback == undefined) {
  132. callback = function () {
  133. // console.log('定位授权::::默认回调函数')
  134. }
  135. }
  136. wx.getSetting({
  137. success(res) {
  138. if (!res.authSetting['scope.userLocation']) {
  139. // 未授权时,先授权
  140. wx.authorize({
  141. scope: 'scope.userLocation',
  142. success(auth) {
  143. console.log("auth:::::", auth);
  144. // 授权成功
  145. if (auth.errMsg == 'authorize:ok') {
  146. // 用户授权成功
  147. that.globalData.location_auth = 1;
  148. // 执行获取地理位置
  149. that.getLocation(callback);
  150. };
  151. },
  152. fail(auth){
  153. console.log("auth:::::", auth);
  154. callback();
  155. // 用户拒绝,添加标识
  156. that.globalData.location_auth = -1;
  157. }
  158. })
  159. } else {
  160. // 已授权,执行获取地理位置
  161. that.globalData.location_auth = 1;
  162. that.getLocation(callback);
  163. }
  164. },
  165. fail(res) {
  166. that.globalData.location_auth = -1;
  167. callback();
  168. console.log('设备信息调取失败',res);
  169. }
  170. })
  171. },
  172. // 执行获取地理位置
  173. getLocation(callback) {
  174. let that = this;
  175. // 用户已经同意小程序使用定位
  176. wx.getLocation({
  177. type: 'wgs84',
  178. success(res) {
  179. console.log('定位成功',res);
  180. that.globalData.lat = res.latitude;
  181. that.globalData.lng = res.longitude;
  182. // console.log('地理位置:', res)
  183. // 执行回调函数
  184. setTimeout(() => {
  185. callback();
  186. }, 0);
  187. },
  188. fail(res){
  189. console.log('定位失败',res);
  190. setTimeout(() => {
  191. callback();
  192. }, 0);
  193. }
  194. })
  195. },
  196. globalData: {
  197. // 测试 domain api imgUrl
  198. // 测试appid: wx3fa706377424c2c9
  199. // domain: "http://singlestore.shanmuzhi.com",
  200. // api: "http://singlestore.shanmuzhi.com/",
  201. // imgUrl: "http://singlestore.shanmuzhi.com",
  202. domain: "https://dandianone.bjycst.top/",
  203. api: "https://dandianone.bjycst.top/",
  204. imgUrl: "https://dandianone.bjycst.top/",
  205. // 正式 wx4ee6b1b6aa3d1403
  206. // domain: 'https://user.jinyutaoxiaoya.com/',
  207. // api: 'https://user.jinyutaoxiaoya.com/',
  208. // imgUrl: 'https://jinyu-txy.oss-cn-beijing.aliyuncs.com',
  209. location_auth: null, // 地理位置授权标识 1同意 -1拒绝
  210. lat: 0, // 衡水市火车站 37.743955
  211. lng: 0, // 衡水市火车站 115.691485
  212. token: wx.getStorageSync('token') ? wx.getStorageSync('token') : '', // ceshitoken
  213. openid: null,
  214. session_key: null,
  215. role: null, // 角色 1为门店 null
  216. userInfo: null,
  217. customTop: null, // 胶囊位置 顶部
  218. customCenter: null, // 胶囊位置 中间
  219. setBgHeight: null, // 导航栏高度
  220. logoTu: "/statics/img/logo.png",
  221. is_member: 0, // 是否为会员 1是 0否
  222. store_id: 1, // 单店(约定id是1)
  223. loadingCount: 0 // 声明一个对象用于存储请求个数
  224. },
  225. });