App.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <script>
  2. let util = require('@/utils/utils.js');
  3. var registerFlag = true;
  4. let crypto = require("crypto");
  5. export default {
  6. globalData:{
  7. userInfo: {},
  8. shopInfo: {
  9. shop_name: "数字茶城",
  10. desc: "数字茶城"
  11. },
  12. kfCode: '',
  13. ShopUrl: 'https://shop.haocha13.cn/ShuZiTeaYW/',
  14. SignUrl: 'https://shop.haocha13.cn/ShuZiTeaIntegral/',
  15. SpecUrl: 'https://shop.haocha13.cn/ShuZiTeaSpecial/',
  16. groupUrl:'https://shop.haocha13.cn/ShuZiTeaSpell/', // 团购测试url tea
  17. // ShopUrl: 'https://tea.soowin.com/ShuZiTeaYW/',
  18. // SignUrl: 'https://tea.soowin.com/ShuZiTeaIntegral/',
  19. // SpecUrl: 'https://tea.soowin.com/ShuZiTeaSpecial/',
  20. // groupUrl:'https://tea.soowin.com/ShuZiTeaSpell/', // 团购测试url tea
  21. // ShopUrl: 'http://192.168.31.176:8080/ShuZiTeaYW/',
  22. // SignUrl: 'http://192.168.31.176:8080/ShuZiTeaIntegral/',
  23. // SpecUrl: 'http://192.168.31.176:8080/ShuZiTeaSpecial/',
  24. // groupUrl:'http://192.168.31.176:8080/ShuZiTeaSpell/', // 团购测试url tea
  25. openId: '',
  26. agentId: 1, //推荐人ID
  27. interfaceheader: {
  28. "Content-Type": "application/x-www-form-urlencoded"
  29. },
  30. authorizeWindow: true, //是否可以打开用户信息授权页面,避免重复打开
  31. bindPhoneWindow: true, //是否可以打开绑定手机号码页面,避免重复打开
  32. hasUpdate: false, // 小程序是否有更新版本 默认为没有
  33. newVersionInfo: {
  34. num: "1.0.0",
  35. desc: "数字茶城上线啦!",
  36. updateInfo: [] // 数组形式描述一下具体更新了什么内容
  37. }, // 当前小程序最新版本信息
  38. msgTmp: null ,//订阅消息模板id信息
  39. systemUserInfo:''
  40. },
  41. //检测缓存中是否有当前用户信息
  42. authorizeUserInfo: function (callback, isHome) {
  43. var that=this
  44. if (this.globalData.systemUserInfo=='') {
  45. uni.getStorage({
  46. key: 'systemUserInfo',
  47. success(res) {
  48. that.globalData.systemUserInfo = res.data;
  49. if (callback) {
  50. callback(res.data.userId);
  51. }
  52. },
  53. fail() {
  54. that.login(callback, isHome);
  55. }
  56. })
  57. } else {
  58. if (callback) {
  59. callback(this.globalData.systemUserInfo.userId);
  60. }
  61. }
  62. },
  63. login: function(callback, isHome) {
  64. var that = this;
  65. uni.showLoading({
  66. icon: 'loading',
  67. mask: true
  68. });
  69. uni.login({
  70. provider: 'weixin',
  71. success: function (res) {
  72. if (res.code) {
  73. // console.log(res.code)
  74. //查询当前用户openId并返回是否已注册
  75. uni.request({
  76. //获取openID
  77. url: that.globalData.ShopUrl + 'login/openid',
  78. method: 'POST',
  79. header: that.globalData.interfaceheader,
  80. data: {
  81. code: res.code
  82. },
  83. success: function (res) {
  84. uni.hideLoading();
  85. if (res.data.status == '200' || res.data.status == '300') {
  86. that.globalData.openId = res.data.openid;
  87. //用户已存在
  88. if (res.data.userInfo && res.data.userInfo != ''){
  89. that.globalData.systemUserInfo = res.data.userInfo;
  90. uni.setStorage({
  91. key: 'systemUserInfo',
  92. data: res.data.userInfo
  93. })
  94. if (callback) {
  95. callback(res.data.userInfo.userId);
  96. }
  97. }else{
  98. //如果不是首页
  99. if (!isHome){
  100. //用户不存在
  101. if(res.data.status == '300'){
  102. let is=1
  103. that.sendauthorizeUserInfo(is,callback);
  104. }else{
  105. let is=1
  106. that.sendauthorizeUserInfo(is,callback);
  107. }
  108. }else{
  109. callback();
  110. }
  111. }
  112. } else {
  113. console.log("登录" + res.data.status);
  114. that.errTips(res.data.msg || "");
  115. }
  116. }
  117. })
  118. } else {
  119. uni.hideLoading();
  120. that.errTips("登录失败,请稍后重试。");
  121. }
  122. },
  123. fail: (err) => {
  124. console.log(err)
  125. }
  126. })
  127. },
  128. setAppData:function(id,data){
  129. this.globalData.systemUserInfo=data
  130. this.globalData.openId=id
  131. },
  132. //发起userInfo授权
  133. sendauthorizeUserInfo: function(is,callback) {
  134. var that = this;
  135. //检测当前微信版本是否兼容wx.getUserInfo
  136. if (uni.getSetting) {
  137. uni.getSetting({
  138. success(res) {
  139. if (res.authSetting['scope.userInfo']) {
  140. if(is==1){
  141. that.setData(callback);
  142. }else if(is==2){
  143. that.setUnionid(callback)
  144. }
  145. } else {
  146. if (that.globalData.authorizeWindow) {
  147. uni.navigateTo({
  148. url: "/pages/authorize/index"
  149. })
  150. that.globalData.authorizeWindow = false;
  151. }
  152. }
  153. },
  154. fail(res) {
  155. uni.showModal({
  156. content: "登录失败,请稍后重试。",
  157. showCancel: false
  158. });
  159. }
  160. })
  161. } else {
  162. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样提示
  163. that.checkEdition();
  164. return;
  165. }
  166. },
  167. //微信版本太旧提示
  168. checkEdition: function() {
  169. uni.showModal({
  170. title: '微信版本太旧',
  171. content: '使用旧版本微信,将无法登录、使用一些功能。请至 微信 -> 设置 或其他可信渠道更新微信。',
  172. showCancel: false,
  173. confirmColor: "#f02f2f",
  174. confirmText: "好",
  175. })
  176. },
  177. //发起userInfo授权
  178. sendauthorizeUserInfo: function(is,callback) {
  179. var that = this;
  180. //检测当前微信版本是否兼容wx.getUserInfo
  181. if (uni.getSetting) {
  182. uni.getSetting({
  183. success(res) {
  184. if (res.authSetting['scope.userInfo']) {
  185. if(is==1){
  186. that.setData(callback);
  187. }else if(is==2){
  188. that.setUnionid(callback)
  189. }
  190. } else {
  191. if (that.globalData.authorizeWindow) {
  192. uni.navigateTo({
  193. url: "/pages/authorize/index"
  194. })
  195. that.globalData.authorizeWindow = false;
  196. }
  197. }
  198. },
  199. fail(res) {
  200. uni.showModal({
  201. content: "登录失败,请稍后重试。",
  202. showCancel: false
  203. });
  204. }
  205. })
  206. } else {
  207. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样提示
  208. that.checkEdition();
  209. return;
  210. }
  211. },
  212. // 获取data
  213. setData(callback){
  214. var that = this;
  215. // 如果改为unionid注册时以下代码加上注释
  216. uni.getUserProfile({
  217. desc: '获取用户注册信息',
  218. lang:'zh_CN',
  219. success: function (res) {
  220. var userinfo = {
  221. nickname: res.userInfo.nickName ? util.emoji_utf16toEntities(res.userInfo.nickName) : '',
  222. headimgurl: res.userInfo.avatarUrl || '',
  223. sex: res.userInfo.gender == 2 ? '女' : '男',
  224. city: res.userInfo.city || '',
  225. province: res.userInfo.province || '',
  226. country: res.userInfo.country || ''
  227. };
  228. console.log(userinfo,"aaaaaaaaa")
  229. return
  230. that.globalData.systemUserInfo = userinfo;
  231. var data = {
  232. agentId: that.globalData.agentId,
  233. openid: that.globalData.openId,
  234. ...userinfo
  235. };
  236. let isdata=1
  237. that.setUnionidApi(data,isdata, callback);
  238. },
  239. fail: function (res) {
  240. console.log(res);
  241. }
  242. })
  243. },
  244. setUnionid:function(callback){
  245. var that=this
  246. // 如果改为unionid注册时以下代码去掉注释
  247. uni.login({
  248. success: function (obj) {
  249. if (obj.code) {
  250. uni.getUserInfo({
  251. withCredentials: true,
  252. success: function (res) {
  253. var data = {
  254. agentId: that.globalData.agentId,
  255. code: obj.code,
  256. encryptedData: res.encryptedData,
  257. iv: res.iv
  258. };
  259. let isUnionid=2
  260. that.setUnionidApi(data,isUnionid, callback);
  261. },
  262. fail: function (res) {
  263. console.log("unionid授权失败:" + res);
  264. }
  265. })
  266. }else{
  267. console.log("unionid登录code获取失败:" + obj);
  268. }
  269. },
  270. fail: function (obj) {
  271. console.log("unionid登录code获取失败:" + obj);
  272. }
  273. })
  274. },
  275. setUnionidApi(data = this.parameter("data"),is, callback){
  276. var that = this;
  277. if(is==1){
  278. uni.request({
  279. url: this.globalData.ShopUrl + 'login/data', // 如果改为unionid注册时改回'login/addUnionid'接口
  280. method: 'POST',
  281. header: this.globalData.interfaceheader,
  282. data: data,
  283. success: function (res) {
  284. if (res.data.status == 200) { //登录成功
  285. var userInfo = that.globalData.systemUserInfo;
  286. userInfo = res.data.userInfo;
  287. that.globalData.systemUserInfo = userInfo;
  288. uni.setStorage({
  289. key: 'systemUserInfo',
  290. data: userInfo
  291. })
  292. if (callback) {
  293. callback(userInfo.userId);
  294. }
  295. } else {
  296. uni.hideLoading();
  297. console.log(res.data.msg || "保存信息接口失败");
  298. }
  299. }
  300. })
  301. }else if(is==2){
  302. uni.request({
  303. url: this.globalData.miniShopUrl + 'login/addUnionid',
  304. method: 'POST',
  305. header: this.globalData.interfaceheader,
  306. data: data,
  307. success: function (res) {
  308. if (res.data.status == 200) { //登录成功
  309. var userInfo = that.globalData.systemUserInfo;
  310. userInfo = res.data.userInfo;
  311. that.globalData.systemUserInfo = userInfo;
  312. // var userinfo = res.data.userInfo;
  313. // that.globalData.systemUserInfo = userinfo;
  314. uni.setStorage({
  315. key: 'systemUserInfo',
  316. data: userInfo
  317. })
  318. if (callback) {
  319. callback(userInfo.userId);
  320. }
  321. } else {
  322. uni.hideLoading();
  323. console.log(res.data.msg||"保存unionid接口失败");
  324. // console.log(res.data.msg || "保存信息接口失败");
  325. }
  326. }
  327. })
  328. }
  329. },
  330. errTips(msg, duration, open){
  331. uni.showToast({
  332. title: msg,
  333. icon:'none',
  334. duration: duration ? duration : 1200,
  335. mask: true
  336. })
  337. },
  338. // 接口未传递有效参数值的错误提示
  339. parameter(authName) {
  340. throw new Error("'" + authName + "' cat\'t be empty!");
  341. },
  342. onLaunch: function() {
  343. var logs = uni.getStorageSync('logs') || []
  344. logs.unshift(Date.now())
  345. uni.setStorageSync('logs', logs);
  346. uni.setStorageSync('mallName', "数字茶城");
  347. },
  348. onShow: function() {
  349. console.log('App Show')
  350. },
  351. onHide: function() {
  352. console.log('App Hide')
  353. }
  354. }
  355. </script>
  356. <style>
  357. /*每个页面公共css */
  358. @import "./app.scss";
  359. </style>