index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { connect } from 'herculex';
  2. import { Ajax, valueToBase64 } from '../utils';
  3. const cardInfoApi = 'api/v1/proxy/isvRequest.healthCard.details';
  4. const openCardApi = 'api/v1/proxy/isvRequest.healthCard.open';
  5. const refreshCardApi = 'api/v1/proxy/isvRequest.healthCard.refresh';
  6. const {
  7. windowHeight: wH,
  8. titleBarHeight: tH,
  9. statusBarHeight: sH
  10. } = my.getSystemInfoSync();
  11. const pageHeight = wH - tH - sH;
  12. Component(connect({})({
  13. data: {
  14. step: -1,
  15. cardInfo: {},
  16. loading: true,
  17. viewHeight: `${pageHeight}px`
  18. },
  19. props: {
  20. componentData: {}
  21. },
  22. async didMount() {
  23. const {
  24. storeConfig
  25. } = this;
  26. const state = storeConfig.state;
  27. const {
  28. userInfo
  29. } = state.$global;
  30. const {
  31. isLogin = false
  32. } = userInfo || {};
  33. /*
  34. * 判断是否已经登录
  35. * */
  36. if (isLogin) {
  37. await this.getCardInfo();
  38. return;
  39. }
  40. this.setData({
  41. loading: false
  42. });
  43. },
  44. methods: {
  45. /*
  46. * 获取当前页面实例
  47. * */
  48. getCurPage() {
  49. const pages = getCurrentPages();
  50. return pages[pages.length - 1];
  51. },
  52. /*
  53. * 打开loading
  54. * */
  55. loadingShow(msg = '') {
  56. my.showLoading({
  57. content: msg
  58. }).then(() => null);
  59. },
  60. /*
  61. * 关闭loading
  62. * */
  63. loadingClose() {
  64. my.hideLoading({
  65. page: this.getCurPage()
  66. }).then(() => null);
  67. },
  68. /*
  69. * 退出页面
  70. * */
  71. onExitPage() {
  72. my.navigateBack();
  73. },
  74. /*
  75. * 获取卡的详情
  76. * */
  77. async getCardInfo() {
  78. let info = null;
  79. let success = true;
  80. try {
  81. // my.showLoading();
  82. info = await Ajax(cardInfoApi); // my.hideLoading();
  83. } catch (e) {
  84. success = false;
  85. this.getCardInfoFail();
  86. }
  87. const _data = {
  88. loading: false,
  89. step: 0
  90. };
  91. if (success && info) {
  92. _data.step = 1;
  93. _data.cardInfo = await this.getInfo(info);
  94. }
  95. this.setData(_data);
  96. },
  97. /*
  98. * 获取卡详情失败
  99. * */
  100. getCardInfoFail() {
  101. my.confirm({
  102. title: '获取卡信息失败!',
  103. cancelButtonText: '取消',
  104. confirmButtonText: '重新获取',
  105. success: ({
  106. confirm
  107. }) => !confirm ? this.onExitPage() : this.getCardInfo()
  108. });
  109. },
  110. /*
  111. * 开卡
  112. * */
  113. async onCardOpen() {
  114. let info = null;
  115. let success = true;
  116. this.loadingShow('领取中..');
  117. try {
  118. info = await Ajax(openCardApi);
  119. } catch (e) {
  120. success = false;
  121. this.onCardOpenFail();
  122. }
  123. const _data = {
  124. loading: false
  125. };
  126. if (success && info) {
  127. _data.step = 1;
  128. _data.cardInfo = await this.getInfo(info);
  129. }
  130. this.setData(_data);
  131. this.loadingClose();
  132. },
  133. /*
  134. * 开卡失败
  135. * */
  136. onCardOpenFail() {
  137. my.confirm({
  138. title: '领取卡片失败!',
  139. cancelButtonText: '取消',
  140. confirmButtonText: '重新领取',
  141. success: async ({
  142. confirm
  143. }) => {
  144. if (confirm) await this.onCardOpen();
  145. }
  146. });
  147. },
  148. /*
  149. * 刷新健康卡
  150. * */
  151. async onRefreshCard(healthCardId) {
  152. let info = null;
  153. let success = true;
  154. const para = {
  155. healthCardId
  156. };
  157. this.setData({
  158. loading: true
  159. });
  160. try {
  161. my.showLoading();
  162. info = await Ajax(refreshCardApi, para);
  163. my.hideLoading();
  164. } catch (e) {
  165. success = false;
  166. this.onRefreshCardFail();
  167. }
  168. const _data = {
  169. loading: false
  170. };
  171. if (success && info) {
  172. _data.cardInfo = await this.getInfo(info);
  173. }
  174. this.setData(_data);
  175. },
  176. /*
  177. * 刷新卡信息失败
  178. * */
  179. onRefreshCardFail() {
  180. my.confirm({
  181. title: '刷新失败',
  182. cancelButtonText: '取消',
  183. confirmButtonText: '重试',
  184. success: async ({
  185. confirm
  186. }) => {
  187. if (confirm) await this.onRefreshCard();
  188. }
  189. });
  190. },
  191. /*
  192. * 组装二维码url数据
  193. * */
  194. async getInfo(_info_) {
  195. const {
  196. qrCode,
  197. qrCodeUrl
  198. } = _info_;
  199. if (!qrCodeUrl) {
  200. _info_.qrCodeUrl = await valueToBase64(qrCode);
  201. }
  202. return _info_;
  203. },
  204. /*
  205. * 点击认证按钮
  206. * */
  207. async onAuthTap() {
  208. /*
  209. * 调用授权接口登录
  210. * */
  211. const type = '$global:onPageLogin';
  212. const isLoginSuccess = await this.dispatch(type);
  213. if (isLoginSuccess) {
  214. await this.onCardOpen();
  215. }
  216. },
  217. toNextPage(page) {
  218. this.setData({
  219. step: page
  220. });
  221. },
  222. onBackCard() {
  223. this.setData({
  224. step: 1
  225. });
  226. }
  227. }
  228. }));