app.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. var api = require('./utils/request');
  2. var util = require('./utils/util');
  3. var siteInfo = require('config.js');
  4. App({
  5. globalData: {
  6. loginuser: [],
  7. wechat_config:[],
  8. config:[],
  9. official_uid: null,
  10. ucode: null
  11. },
  12. /**
  13. * 生命周期函数--监听小程序初始化
  14. */
  15. onLaunch: function (options) {
  16. this.api_root = siteInfo.siteroot; //设置api地址
  17. this.appname = siteInfo.name; //设置应用名称
  18. this.updateMiniapp(); //小程序更新
  19. this.setNavigation(); //工具栏
  20. this.setConfig(); //设置小程序配置
  21. },
  22. /**
  23. * 生命周期函数--监听小程序显示
  24. */
  25. onShow: function () {
  26. let that = this;
  27. //登录用户信息
  28. var loginuser = wx.getStorageSync('loginuser');
  29. if (!util.isNull(loginuser)) {
  30. that.globalData.loginuser = loginuser;
  31. }
  32. },
  33. //设置小程序配置
  34. setConfig: function () {
  35. let that = this;
  36. //微信配置
  37. return new Promise((resolve, reject) => {
  38. that.getConfig(function (data) {
  39. wx.setStorageSync('wechat_config', data);
  40. that.globalData.wechat_config = data;
  41. resolve(that.globalData.wechat_config);
  42. });
  43. })
  44. },
  45. //读取小程序配置
  46. getConfig: function (callback) {
  47. api.Get('openapi/v1/config', function (result) {
  48. callback && callback(result.data);
  49. })
  50. },
  51. //用全局变量保存邀请码
  52. setUcode: function (options) {
  53. let that = this;
  54. if (options.scene) {
  55. var scene = util.strToArray(decodeURIComponent(options.scene));
  56. if (!util.isNull(scene.ucode)) {
  57. that.globalData.ucode = scene.ucode;
  58. }
  59. } else {
  60. if (!util.isNull(options.ucode)) {
  61. that.globalData.ucode = options.ucode;
  62. }
  63. }
  64. },
  65. //当前用户id
  66. getUserId: function () {
  67. return this.globalData.loginuser.uid;
  68. },
  69. //判断是否登录
  70. isLogin: function (callback) {
  71. let session_id = wx.getStorageSync('session_id'),
  72. loginuser = wx.getStorageSync('loginuser'),
  73. token = wx.getStorageSync('token')
  74. wx.checkSession({
  75. success(rel) {
  76. if (util.isNull(session_id) || util.isNull(token) || util.isNull(loginuser)) {
  77. wx.navigateTo({
  78. url: '/pages/helper/login'
  79. })
  80. } else {
  81. callback && callback(true);
  82. }
  83. },
  84. fail(rel) {
  85. wx.navigateTo({
  86. url: '/pages/helper/login'
  87. })
  88. }
  89. })
  90. },
  91. /**
  92. * 微信小程序登录
  93. */
  94. doLogin: function (options, callback) {
  95. let that = this;
  96. wx.login({
  97. success: function (res) {
  98. api.Post('openapi/v1/login', {
  99. code: res.code,
  100. user_info: options.detail.rawData,
  101. encrypted_data: options.detail.encryptedData,
  102. iv: options.detail.iv,
  103. signature: options.detail.signature,
  104. official_uid: wx.getStorageSync('official_uid'),
  105. invite_code: that.globalData.ucode
  106. }, function (result) {
  107. wx.setStorageSync('token', result.data.token);
  108. wx.setStorageSync('session_id', result.data.session_id);
  109. wx.setStorageSync('loginuser', result.data);
  110. that.globalData.loginuser = result.data;
  111. callback && callback(true);
  112. })
  113. },
  114. fail: function () {
  115. callback && callback(false);
  116. }
  117. });
  118. },
  119. /**
  120. * 调用微信支付
  121. */
  122. doWechatPay: function (data, successCallback, failCallback) {
  123. var dataMap = {
  124. timeStamp: data.timestamp,
  125. nonceStr: data.nonceStr,
  126. package: data.package,
  127. signType: data.signType,
  128. paySign: data.paySign,
  129. success: successCallback,
  130. fail: failCallback
  131. }
  132. wx.requestPayment(dataMap);
  133. },
  134. /**
  135. * 更新小程序
  136. */
  137. updateMiniapp: function () {
  138. if (wx.canIUse('getUpdateManager')) {
  139. const updateManager = wx.getUpdateManager()
  140. //判断是否有新的小程序
  141. updateManager.onCheckForUpdate(function (res) {
  142. if (res.hasUpdate) {
  143. updateManager.onUpdateReady(function () {
  144. wx.clearStorage() //清理缓存
  145. wx.showModal({
  146. showCancel: false,
  147. title: '更新提示',
  148. content: '新版本已经升级完成,请重启应用!',
  149. success: function (res) {
  150. updateManager.applyUpdate()
  151. }
  152. })
  153. })
  154. //新的版本下载失败
  155. updateManager.onUpdateFailed(function () {
  156. wx.showModal({
  157. showCancel: false,
  158. title: '已经有新版本了哟~',
  159. content: '新版本自动升级失败了~请您删除当前小程序,重新搜索打开哟~',
  160. success: function (res) {
  161. updateManager.applyUpdate()
  162. }
  163. })
  164. })
  165. }
  166. })
  167. } else {
  168. wx.showModal({
  169. title: '提示',
  170. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  171. })
  172. }
  173. },
  174. /**
  175. * Navgation导航的高度
  176. */
  177. setNavigation() {
  178. let that = this
  179. wx.getSystemInfo({
  180. success: function (e) {
  181. that.globalData.startBarHeight = e.statusBarHeight;
  182. if (e.platform == 'android') {
  183. that.globalData.navgationHeight = 50;
  184. } else {
  185. that.globalData.navgationHeight = 45;
  186. };
  187. }
  188. })
  189. },
  190. /**
  191. * 调用购物车图标
  192. */
  193. setTabBarCartNumber: function () {
  194. var cartItemNumber = wx.getStorageSync('shopping_num')
  195. if (cartItemNumber > 0 && !this.util().isNull(cartItemNumber)) {
  196. wx.setTabBarBadge({
  197. index: 2,
  198. text: cartItemNumber.toString()
  199. })
  200. } else {
  201. wx.removeTabBarBadge({
  202. index: 2,
  203. })
  204. }
  205. },
  206. /**
  207. * 调用API
  208. */
  209. api: function () {
  210. return api;
  211. },
  212. util: function () {
  213. return util;
  214. }
  215. });