add-step1.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. var d = dialog({contentType:'load', skin:'bk-popup'});
  2. var dd = dialog({contentType:'load', skin:'bk-popup', content:'提交中...'});
  3. var relation;
  4. var family = [];
  5. $(function(){
  6. // 判断是否登录
  7. checkUserAgent();
  8. getFamilyMembersPromise()
  9. .then(function(res) {
  10. if(res.status == 200) {
  11. family = res.data.normalmembers;
  12. } else {
  13. dialog({contentType:'tipsbox', skin:'bk-popup' , content: res.msg||'数据获取失败'}).show();
  14. }
  15. })
  16. .catch(function(e) {
  17. console && console.error(e)
  18. })
  19. // 选择家庭关系
  20. $('#select').mobiscroll({
  21. theme: 'ios',
  22. lang: 'zh',
  23. formatValue: function(d) {
  24. return d;
  25. },
  26. customWheels: true,
  27. wheels: [
  28. [{
  29. keys: ['1', '2', '3', '4', '5', '6', '0'],
  30. values: ['父亲', '母亲', '老公', '老婆', '儿子', '女儿', '其他']
  31. }]
  32. ],
  33. onSelect: function(valueText, inst) {
  34. var dd = eval("[" + valueText + "]");
  35. $('#select').val(dd[0].values);
  36. relation = dd[0].keys;
  37. }
  38. });
  39. // 下一步
  40. $("#next").bind("tap", function () {
  41. if(!$(this).hasClass('btn-disabled')) {
  42. commit();
  43. }
  44. });
  45. });
  46. function checkVal ( cv, attr, d) {
  47. var len = d.length,
  48. sta = 1;
  49. for ( var i = 0; i < len; i++) {
  50. if (d[i][attr] == cv) {
  51. sta = -1;
  52. break;
  53. }
  54. }
  55. return sta;
  56. }
  57. // 禁止拖动
  58. document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
  59. function getFamilyMembersPromise() {
  60. d.show();
  61. return new Promise(function(resolve, reject){
  62. sendPost("patient/family/members", {}, "json", "get", function fail() {
  63. d.close();
  64. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'数据获取失败'}).show();
  65. }, function success(res) {
  66. d.close();
  67. resolve(res)
  68. });
  69. })
  70. }
  71. //验证信息
  72. function validate(data) {
  73. if (data.name.trim() == "") {
  74. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'请输入姓名'}).show();
  75. return false;
  76. }
  77. if (!isIdcard(data.idcard)) {
  78. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'身份证格式不正确'}).show();
  79. return false;
  80. }
  81. if (data.select.trim() == "") {
  82. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'请选择家庭关系'}).show();
  83. return false;
  84. }
  85. //判断家庭关系与身份证性别是否匹配
  86. var d = data.idcard[data.idcard.length - 2],
  87. isMan = d%2 == 1;
  88. if(isMan && (relation==2 || relation==4 || relation==6)){
  89. dialog({
  90. contentType:'tipsbox',
  91. skin:'bk-popup',
  92. content: "您的家人性别与家庭关系不匹配,请核对后再确认"
  93. }).show();
  94. return false;
  95. }
  96. if(!isMan && (relation==1 || relation==3 || relation==5)){
  97. dialog({
  98. contentType:'tipsbox',
  99. skin:'bk-popup',
  100. content: "您的家人性别与家庭关系不匹配,请核对后再确认"
  101. }).show();
  102. return false;
  103. }
  104. if (checkVal( data.name, 'name', family) == -1 || checkVal( data.idcard, 'idcard', family) == -1) {
  105. dialog({contentType:'tipsbox', skin:'bk-popup' , content: data.name + '已是您的家庭成员咯~'}).show();
  106. return false;
  107. }
  108. return true;
  109. }
  110. // 提交失败
  111. function commitFailed (res) {
  112. dd.close();
  113. if (res && res.msg) {
  114. var d1 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg}).show();
  115. } else {
  116. var d2 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:'加载失败'}).show();
  117. }
  118. }
  119. function checkMemberPromise(code) {
  120. return new Promise(function(resolve, reject) {
  121. sendPost("patient/family/check_member", {
  122. memberCode: code
  123. }, "json", "get", commitFailed, function(res) {
  124. resolve(res)
  125. });
  126. })
  127. }
  128. function sendWXTempPromise(data) {
  129. return new Promise(function(resolve, reject) {
  130. sendPost("patient/family/sendWXTemp", data , "json", "POST", commitFailed, function(res) {
  131. resolve(res)
  132. });
  133. })
  134. }
  135. // 提交成功
  136. function commitSuccess (res) {
  137. if (res.status == 200) {
  138. var data = res.data;
  139. if (data.isRegister == -1) {
  140. dd.close();
  141. $('#next').removeClass("btn-disabled");
  142. var d4 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:'别闹了,这是您自己,快添加您的家人吧'}).show();
  143. } else if (data.isRegister == 0) {
  144. dd.close();
  145. $('#next').removeClass("btn-disabled");
  146. dialog({
  147. content:'对不起,目前仅支持添加注册用户为家人哦,是否推荐家人注册?',
  148. okValue: '前往推荐',
  149. ok: function (){
  150. // 跳转分享
  151. window.location.href = '../html/family-share.html';
  152. },
  153. cancel: function () {}
  154. }).showModal();
  155. } else {
  156. if (data.name != $("#name").val()) {
  157. dd.close();
  158. $('#next').removeClass("btn-disabled");
  159. var d4 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:'姓名与身份证不一致'}).show();
  160. } else {
  161. window.localStorage.removeItem("familyCode");
  162. window.localStorage.setItem("familyCode", data.code);
  163. window.localStorage.removeItem("familyName");
  164. window.localStorage.setItem("familyName", data.name);
  165. window.localStorage.removeItem("familyImgUrl");
  166. window.localStorage.setItem("familyImgUrl", getImgUrl(data.photo));
  167. window.localStorage.removeItem("familyRelation");
  168. window.localStorage.setItem("familyRelation", relation);
  169. checkMemberPromise(data.code)
  170. .then(function(res) {
  171. dd.close();
  172. if(res.status == "200") {
  173. var state = res.data.state;
  174. if(state == "0") { // 未绑定微信和手机
  175. $('#next').removeClass("btn-disabled");
  176. window.location.href = 'add-step3.html';
  177. } else if(state == "1") { // 仅绑定微信
  178. sendWXTempPromise({
  179. member: data.code,
  180. relation: relation
  181. })
  182. .then(function(res) {
  183. if(res.status=="200") {
  184. $('#next').addClass("btn-disabled");
  185. dialog({contentType:'tipsbox', skin:'bk-popup' , content:"已发送添加申请"}).show();
  186. } else {
  187. $('#next').removeClass("btn-disabled");
  188. if(res.msg){
  189. dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg}).show();
  190. }else
  191. dialog({contentType:'tipsbox', skin:'bk-popup' , content:"发送失败"}).show();
  192. }
  193. })
  194. } else if(state == "2") { // 仅绑定手机
  195. $('#next').removeClass("btn-disabled");
  196. window.localStorage.removeItem("familyMobile");
  197. window.localStorage.setItem("familyMobile", data.mobile);
  198. window.location.href = 'add-step2.html';
  199. } else if(state== "3") { // 手机和微信都绑定
  200. $('#next').removeClass("btn-disabled");
  201. window.localStorage.removeItem("familyMobile");
  202. window.localStorage.setItem("familyMobile", data.mobile);
  203. window.location.href = 'add-step2-pre.html';
  204. }
  205. } else {
  206. dd.close();
  207. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'加载失败'}).show()
  208. }
  209. })
  210. .catch(function(e) {
  211. dd.close();
  212. console && console.error(e)
  213. })
  214. // if (data.mobile == "") {
  215. // window.location.href = 'add-step3.html';
  216. // } else {
  217. // window.localStorage.removeItem("familyMobile");
  218. // window.localStorage.setItem("familyMobile", data.mobile);
  219. // window.location.href = 'add-step2.html';
  220. // }
  221. }
  222. }
  223. } else {
  224. //非200则为失败
  225. commitFailed(res);
  226. }
  227. }
  228. // 提交添加家人
  229. function commit() {
  230. var data = {};
  231. data.name = $("#name").val();
  232. data.idcard = $("#idcard").val();
  233. data.select = $("#select").val();
  234. if (validate(data)) {
  235. // 验证通过执行
  236. dd.show();
  237. $('#next').addClass("btn-disabled");
  238. sendPost("patient/family/is_register", data, "json", "get", commitFailed, commitSuccess);
  239. }
  240. }