request.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. import * as config from '@/config'
  2. import WXBizDataCrypt from './WXBizDataCrypt.js'
  3. let baseUrl = config.def().baseUrlNew
  4. // 定义基础请求路径(后端服务器地址)
  5. const baseRequest = (_gp, _mt, data = {}, failCallback) => {
  6. //异步请求数据
  7. return new Promise(resolve => {
  8. // if (!userInfo || !userInfo.accessToken) {
  9. // userInfo = uni.getStorageSync('userInfo')
  10. // }
  11. // let accessToken = userInfo ? userInfo.accessToken : ''
  12. let baseUrl = config.def().baseUrlNew
  13. uni.request({
  14. url: baseUrl + '/m.api',
  15. data: {
  16. ...data,
  17. _gp,
  18. _mt
  19. },
  20. method: 'POST',
  21. header: {
  22. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  23. // 'ACCESSTOKEN': accessToken
  24. },
  25. success: (res) => {
  26. if (res.statusCode === 200) {
  27. if (res.data.errno === 200) {
  28. resolve(res.data);
  29. } else {
  30. if (failCallback) {
  31. failCallback(res.data)
  32. } else {
  33. uni.showToast({
  34. title: res.data.errmsg,
  35. icon: 'none'
  36. })
  37. }
  38. }
  39. }
  40. }
  41. })
  42. })
  43. }
  44. //带Token请求
  45. const TokenRequest = (method, url, data, header) => {
  46. var contentheader = 'application/json'
  47. if (header) {
  48. contentheader = header
  49. }
  50. let ac_token = "";
  51. uni.getStorage({
  52. key: 'userInfo',
  53. success: function(res) {
  54. ac_token = res.data.accessToken
  55. }
  56. });
  57. //此token是登录成功后后台返回保存在storage中的
  58. let DefaultOpts = {
  59. url: baseUrl + url,
  60. data: data,
  61. method: method,
  62. header: {
  63. 'content-type': contentheader,
  64. 'Token': ac_token,
  65. }
  66. }
  67. let promise = new Promise(function(resolve, reject) {
  68. uni.request(DefaultOpts).then(
  69. (res) => {
  70. console.log(JSON.stringify(res[1].data))
  71. if (res[1].data.code == '200' || res[1].data.code == 200) {
  72. // 后端返回的状态码100为成功状态,成功则返回请求结果,在app调试时可以通过console.log(JSON.stringify(res[1].data))来查看返回值(以项目实际情况为准)
  73. resolve(res[1].data)
  74. }
  75. if (res[1].data.code == '105' || res[1].data.code == 105) {
  76. // 后端返回状态码为105则为未登录状态(以项目实际情况为准)
  77. uni.showToast({
  78. icon: 'none',
  79. title: '尚未登录',
  80. duration: 2000
  81. });
  82. // 尚未登录的逻辑处理
  83. return false
  84. }
  85. }
  86. ).catch(
  87. (response) => {
  88. reject(response)
  89. }
  90. )
  91. })
  92. return promise
  93. }
  94. //微信登录
  95. const wxlogin = () => {
  96. let promise = new Promise(function(resolve, reject) {
  97. uni.login({
  98. "provider": "weixin",
  99. success: function(wxres) {
  100. console.log("wxlogin",wxres)
  101. baseRequest('commonUserApp', 'commonUserLogin', {
  102. loginType: 1,
  103. raw: JSON.stringify(wxres)
  104. }, failres => {
  105. uni.showToast({
  106. icon: "none",
  107. title: failres.errmsg,
  108. duration: 3000
  109. });
  110. uni.hideLoading()
  111. }).then(res => {
  112. console.log(res.data)
  113. resolve(res.data)
  114. })
  115. },
  116. fail: function(err) {
  117. reject(err.code)
  118. }
  119. })
  120. })
  121. return promise
  122. }
  123. //获取手机号
  124. const getPhone = (e, userInfo) => {
  125. let promise = new Promise(function(resolve, reject) {
  126. if (e.mp.detail.errMsg === "getPhoneNumber:ok") {
  127. let appId = 'wx5d8906c2208c899f'
  128. let sessionKey = userInfo.sessionKey
  129. let encryptedData = e.mp.detail.encryptedData
  130. let iv = e.mp.detail.iv
  131. let data =''
  132. try {
  133. console.log('解密前 encryptedData: ', encryptedData)
  134. console.log('解密前 iv: ', iv)
  135. console.log('解密前 appId: ', appId)
  136. console.log('解密前 sessionKey: ', sessionKey)
  137. data= WXBizDataCrypt.prototype.decryptData(encryptedData, iv, appId, sessionKey)
  138. console.log('解密后 data: ', data)
  139. resolve(data.phoneNumber)
  140. } catch (e) {
  141. uni.showToast({
  142. icon: "none",
  143. title: e,
  144. duration: 3000
  145. });
  146. }
  147. }
  148. })
  149. return promise
  150. }
  151. //同步信息
  152. const syncInfo = (userInfo) => {
  153. let promise = new Promise(function(resolve, reject) {
  154. debugger
  155. if (!userInfo.head) userInfo.head =
  156. 'https://taohaoliang.oss-cn-beijing.aliyuncs.com/app/card_head.png'
  157. if (!userInfo.nickname) {
  158. let code = ""
  159. for (var i = 0; i < 6; i++) {
  160. code += parseInt(Math.random() * 10);
  161. }
  162. userInfo.nickname = 'user' + code
  163. }
  164. baseRequest('commonUserApp', 'edit', {
  165. commonUserInfo: JSON.stringify(userInfo)
  166. }, failres => {
  167. uni.showToast({
  168. icon: "none",
  169. title: failres.errmsg,
  170. duration: 3000
  171. });
  172. reject(err.code)
  173. }).then(res => {
  174. resolve(res.data)
  175. })
  176. })
  177. return promise
  178. }
  179. // 处理海报数据
  180. const makeCanvasData = (check_idx,height,posterObj1,theight) => {
  181. console.log(height,theight)
  182. var dth = (theight - 22) * 2
  183. var h=((height+40)*2)
  184. console.log(posterObj1)
  185. var posterObj= {}
  186. switch (check_idx) {
  187. case 0:
  188. posterObj={
  189. width: '670rpx',
  190. height: h+'rpx',
  191. background: '#fff',
  192. borderRadius: '16rpx',
  193. views:[
  194. {
  195. type: 'text',
  196. text: posterObj1.title,
  197. use:'title',
  198. css: {
  199. fontSize: '32rpx',
  200. color: '#1A2033',
  201. fontWeight: 'bold',
  202. lineHeight: '45rpx',
  203. left: '32rpx',
  204. top: '410rpx',
  205. width: '606rpx'
  206. }
  207. },
  208. {
  209. type: 'text',
  210. text: posterObj1.title1,
  211. use:'title1',
  212. css: {
  213. fontSize: '28rpx',
  214. fontWeight: 'bold',
  215. color: '#1A2033',
  216. lineHeight: '28rpx',
  217. left: '32rpx',
  218. top: 535+dth+'rpx'
  219. }
  220. },
  221. {
  222. type: 'text',
  223. text: '长按或扫描识别二维码',
  224. use:'title2',
  225. css: {
  226. fontSize: '24rpx',
  227. color: '#4070FF',
  228. lineHeight: '24rpx',
  229. left: '32rpx',
  230. top: 579+dth+'rpx'
  231. }
  232. },
  233. {
  234. type: 'image',
  235. src: posterObj1.image,
  236. use:'dt',
  237. css: {
  238. left: '32rpx',
  239. top: '44rpx',
  240. width: '606rpx',
  241. height: '341rpx',
  242. borderRadius: '16rpx'
  243. }
  244. },
  245. {
  246. type: 'image',
  247. src: posterObj1.qrcode,
  248. use:'qrcode',
  249. css: {
  250. left: '455rpx',
  251. top: 520+dth+'rpx',
  252. // transform: 'translate(-50%,0)',
  253. width: '240rpx',
  254. height: '240rpx',
  255. }
  256. },
  257. // {
  258. // type: 'text',
  259. // text: '———— 由云现场提供技术支持 ————',
  260. // css: {
  261. // left: '0',
  262. // top: '863rpx',
  263. // width: '100%',
  264. // textAlign: 'center',
  265. // fontSize: '24rpx',
  266. // color: '#989FB3',
  267. // lineHeight: '33rpx'
  268. // }
  269. // }
  270. ],
  271. }
  272. break;
  273. case 1:
  274. posterObj={
  275. width: '670rpx',
  276. height: h+'rpx',
  277. background: '#fff',
  278. borderRadius: '16rpx',
  279. views: [{
  280. type: 'image',
  281. use:'bg',
  282. src: 'https://s.yun-live.com/images/20210201/39ae4d9d8ad0b1acac7c224e845c641f.png',
  283. css: {
  284. left: '0',
  285. top: '0',
  286. width: '100%',
  287. height: '100%'
  288. }
  289. },
  290. {
  291. type: 'image',
  292. src: posterObj1.image,
  293. css: {
  294. left: '32rpx',
  295. top: '44rpx',
  296. width: '606rpx',
  297. height: '341rpx',
  298. borderRadius: '16rpx'
  299. }
  300. },
  301. {
  302. type: 'text',
  303. text: posterObj1.title,
  304. use:'title',
  305. css: {
  306. fontSize: '32rpx',
  307. color: '#fff',
  308. fontWeight: 'bold',
  309. lineHeight: '45rpx',
  310. left: '32rpx',
  311. top: '420rpx',
  312. width: '590rpx'
  313. }
  314. },
  315. {
  316. type: 'text',
  317. text:posterObj1.title1,
  318. use:'title1',
  319. css: {
  320. fontSize: '28rpx',
  321. fontWeight: 'bold',
  322. color: '#D8AB87',
  323. lineHeight: '28rpx',
  324. left: '32rpx',
  325. top: 535+dth+'rpx'
  326. }
  327. },
  328. {
  329. type: 'text',
  330. text: '长按或扫描识别二维码',
  331. use:'title2',
  332. css: {
  333. fontSize: '24rpx',
  334. color: '#FFFFFF',
  335. lineHeight: '24rpx',
  336. left: '32rpx',
  337. top: 579+dth+'rpx'
  338. }
  339. },
  340. {
  341. type: 'image',
  342. src: 'https://s.yun-live.com/images/20210201/d88d56843d43b917e2a28550b2a62723.png',
  343. css: {
  344. left: '0rpx',
  345. top: '90%',
  346. width: '103rpx',
  347. height: '103rpx',
  348. }
  349. },
  350. {
  351. type: 'image',
  352. src: 'https://s.yun-live.com/images/20210201/247736ffd279276b891ec14db8ed0fd0.png',
  353. css: {
  354. left: '600rpx',
  355. top: '50%',
  356. width: '56.4rpx',
  357. height: '56.4rpx',
  358. }
  359. },
  360. {
  361. type: 'view',
  362. use:'qrcode-view',
  363. css: {
  364. left: '445rpx',
  365. top: 520+dth+'rpx',
  366. width: '240rpx',
  367. height: '240rpx',
  368. background: '#fff',
  369. }
  370. },
  371. {
  372. type: 'image',
  373. src: posterObj1.qrcode,
  374. use:'qrcode',
  375. css: {
  376. left: '455rpx',
  377. top: 530+dth+'rpx',
  378. // transform: 'translate(-50%,0)',
  379. width: '220rpx',
  380. height: '220rpx',
  381. }
  382. },
  383. ]
  384. }
  385. break;
  386. case 2:
  387. posterObj={
  388. width: '670rpx',
  389. height: h+'rpx',
  390. background: '#fff',
  391. borderRadius: '16rpx',
  392. views: [{
  393. type: 'image',
  394. use:'bg',
  395. src: 'https://s.yun-live.com/images/20210201/78f227bd701da20676c9da9166ce3144.png',
  396. css: {
  397. left: '0',
  398. top: '0',
  399. width: '100%',
  400. height: '100%'
  401. }
  402. },
  403. {
  404. type: 'image',
  405. src: posterObj1.image,
  406. css: {
  407. left: '62rpx',
  408. top: '84rpx',
  409. width: '540rpx',
  410. height: '304rpx',
  411. borderRadius: '16rpx'
  412. }
  413. },
  414. {
  415. type: 'text',
  416. text: posterObj1.title,
  417. use:'title',
  418. css: {
  419. fontSize: '32rpx',
  420. color: '#1D1D25',
  421. fontWeight: 'bold',
  422. lineHeight: '45rpx',
  423. left: '62rpx',
  424. top: '420rpx',
  425. width: '530rpx'
  426. }
  427. },
  428. {
  429. type: 'text',
  430. text:posterObj1.title1,
  431. use:'title1',
  432. css: {
  433. fontSize: '28rpx',
  434. fontWeight: 'bold',
  435. color: '#1D1D25',
  436. lineHeight: '28rpx',
  437. left: '62rpx',
  438. top: 515+dth+'rpx'
  439. }
  440. },
  441. {
  442. type: 'text',
  443. text: '长按或扫描识别二维码',
  444. use:'title2',
  445. css: {
  446. fontSize: '24rpx',
  447. color: '#6CB37A',
  448. lineHeight: '24rpx',
  449. left: '62rpx',
  450. top: 559+dth+'rpx'
  451. }
  452. },
  453. {
  454. type: 'view',
  455. use:'qrcode-view',
  456. css: {
  457. left: '405rpx',
  458. top: 500+dth+'rpx',
  459. width: '240rpx',
  460. height: '240rpx',
  461. background: '#fff',
  462. }
  463. },
  464. {
  465. type: 'image',
  466. src: posterObj1.qrcode,
  467. use:'qrcode',
  468. css: {
  469. left: '415rpx',
  470. top: 510+dth+'rpx',
  471. // transform: 'translate(-50%,0)',
  472. width: '220rpx',
  473. height: '220rpx',
  474. }
  475. },]
  476. }
  477. break;
  478. case 3:
  479. posterObj={
  480. width: '670rpx',
  481. height: h+'rpx',
  482. background: '#fff',
  483. borderRadius: '16rpx',
  484. views: [{
  485. type: 'image',
  486. use:'bg',
  487. src: 'https://s.yun-live.com/images/20210201/524ab6a41fe8c7eb57b35df9a547d388.png',
  488. css: {
  489. left: '0',
  490. top: '0',
  491. width: '100%',
  492. height: '100%'
  493. }
  494. },
  495. {
  496. type: 'image',
  497. src: posterObj1.image,
  498. css: {
  499. left: '32rpx',
  500. top: '44rpx',
  501. width: '606rpx',
  502. height: '341rpx',
  503. borderRadius: '16rpx'
  504. }
  505. },
  506. {
  507. type: 'text',
  508. text: posterObj1.title,
  509. use:'title',
  510. css: {
  511. fontSize: '32rpx',
  512. color: '#1D1D25',
  513. fontWeight: 'bold',
  514. lineHeight: '45rpx',
  515. left: '32rpx',
  516. top: '410rpx',
  517. width: '530rpx'
  518. }
  519. },
  520. {
  521. type: 'text',
  522. text:posterObj1.title1,
  523. use:'title1',
  524. css: {
  525. fontSize: '28rpx',
  526. fontWeight: 'bold',
  527. color: '#1D1D25',
  528. lineHeight: '28rpx',
  529. left: '32rpx',
  530. top: 525+dth+'rpx'
  531. }
  532. },
  533. {
  534. type: 'text',
  535. text: '长按或扫描识别二维码',
  536. use:'title2',
  537. css: {
  538. fontSize: '24rpx',
  539. color: '#6CB37A',
  540. lineHeight: '24rpx',
  541. left: '32rpx',
  542. top: 569+dth+'rpx'
  543. }
  544. },
  545. {
  546. type: 'view',
  547. use:'qrcode-view',
  548. css: {
  549. left: '440rpx',
  550. top: 500+dth+'rpx',
  551. width: '240rpx',
  552. height: '240rpx',
  553. background: '#fff',
  554. }
  555. },
  556. {
  557. type: 'image',
  558. src: posterObj1.qrcode,
  559. use:'qrcode',
  560. css: {
  561. left: '450rpx',
  562. top: 510+dth+'rpx',
  563. // transform: 'translate(-50%,0)',
  564. width: '220rpx',
  565. height: '220rpx',
  566. }
  567. },]
  568. }
  569. // this.posterObj1.backgroundType='image'
  570. // this.posterObj1.background='https://s.yun-live.com/images/20210201/524ab6a41fe8c7eb57b35df9a547d388.png';
  571. // this.posterObj1.width='606rpx',
  572. // this.posterObj1.height='341rpx',
  573. // this.posterObj1.titleCssColor='#1D1D25'
  574. // this.posterObj1.title1CssColor='#1D1D25'
  575. // this.posterObj1.title2CssColor='#6CB37A'
  576. // break;
  577. default:
  578. break;
  579. }
  580. return posterObj
  581. }
  582. export default {
  583. baseUrl,
  584. baseRequest,
  585. TokenRequest,
  586. wxlogin,
  587. getPhone,
  588. syncInfo,
  589. makeCanvasData
  590. }