Bind.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Bind.php UTF-8
  4. *
  5. *
  6. * @date : 2018/5/4 11:57
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\member;
  13. use huo\controller\common\Base;
  14. use huo\controller\integral\MemIa;
  15. use huo\model\member\MemberModel;
  16. use huolib\constant\IaConst;
  17. use huolib\sms\Sms;
  18. use huolib\status\MemberStatus;
  19. use huolib\tool\StrUtils;
  20. use think\Session;
  21. class Bind extends Base {
  22. /**
  23. * 获取已绑定的手机号
  24. *
  25. * @param $mem_id
  26. *
  27. * @return null
  28. */
  29. public function getHasBindMobile($mem_id) {
  30. $_mem_info = (new MemCache())->getInfoById($mem_id);
  31. if (empty($_mem_info) || empty($_mem_info['mobile'])) {
  32. return null;
  33. }
  34. $_mobile = $_mem_info['mobile'];
  35. $this->setHasBind($_mobile);
  36. return $_mobile;
  37. }
  38. /**
  39. * 发送手机短信
  40. *
  41. * @param string $mobile
  42. * @param int $type 4 校验 6 绑定手机
  43. *
  44. * @return array
  45. */
  46. public function smsSend($mobile, $type) {
  47. if (!$this->getCheckOk()) {
  48. if ($this->getHasBind()) {
  49. $mobile = $this->getOldMobile();
  50. }
  51. }
  52. $_rs = (new Sms())->send($mobile, $type);
  53. if (MemberStatus::NO_ERROR == $_rs['code']) {
  54. return $this->huoError($_rs['code'], $_rs['msg'], $_rs['data']);
  55. }
  56. return $this->huoSuccess($_rs['code'], $_rs['msg'], $_rs['data']);
  57. }
  58. /**
  59. * 校验原有手机
  60. *
  61. * @param $mobile
  62. * @param $code
  63. * @param $type
  64. */
  65. public function checkOldMobile($mobile, $code, $type) {
  66. if ($this->getHasBind()) {
  67. $mobile = $this->getOldMobile();
  68. }
  69. /* 校验短信是否正确 */
  70. $_sms_rs = (new Sms())->check($mobile, $code, $type);
  71. if (MemberStatus::NO_ERROR != $_sms_rs['code']) {
  72. return $this->huoError($_sms_rs['code'], $_sms_rs['msg']);
  73. }
  74. $this->setCheckOk();
  75. $_code = MemberStatus::NO_ERROR;
  76. return $this->huoSuccess($_code, MemberStatus::getMsg($_code));
  77. }
  78. /**
  79. * 绑定手机中更新信息
  80. *
  81. * @param int $mem_id
  82. * @param string $mobile
  83. * @param string $code
  84. * @param int $type
  85. * @param string $password
  86. *
  87. * @return array
  88. */
  89. public function bindPost($mem_id, $mobile, $code, $type, $password = '') {
  90. /* 屏蔽密码 20180130
  91. $_chk_pwd_rs = UserUtils::checkPassword($password);
  92. if (MemberStatus::NO_ERROR != $_chk_pwd_rs) {
  93. return $this->huoError($_chk_pwd_rs, MemberStatus::getMsg($_chk_pwd_rs));
  94. }
  95. */
  96. /* 校验密码是否正确 */
  97. $_mem_class = new Member();
  98. $_mem_data = (new MemCache())->getInfoById($mem_id);
  99. if (false == $this->getCheckOk() || !empty($_mem_data['password'])) {
  100. /* 绑定需要输入密码 */
  101. /* 屏蔽密码 20180130
  102. $_chk_pwd = $_mem_class->checkPwd($password, $_mem_data['password']);
  103. if (false == $_chk_pwd) {
  104. $_code = MemberStatus::USERNAME_OR_PASSWORD_ERR;
  105. return $this->huoError($_code, MemberStatus::getMsg($_code));
  106. }
  107. */
  108. if (empty($mobile)) {
  109. $_code = MemberStatus::PHONE_ERROR;
  110. return $this->huoError($_code, MemberStatus::getMsg($_code));
  111. }
  112. } else {
  113. $password = '';
  114. }
  115. /* 有绑定手机 请先校验原有手机 */
  116. if ($this->getHasBind() && false == $this->getCheckOk()) {
  117. $_code = MemberStatus::PHONE_NOT_CHECK;
  118. return $this->huoError($_code, MemberStatus::getMsg($_code));
  119. }
  120. /* 校验短信是否正确 */
  121. $_sms_rs = (new Sms())->check($mobile, $code, $type);
  122. if (MemberStatus::NO_ERROR != $_sms_rs['code']) {
  123. return $this->huoError($_sms_rs['code'], $_sms_rs['msg']);
  124. }
  125. $_rs = $this->updateData($mem_id, $mobile);
  126. if (MemberStatus::NO_ERROR != $_rs) {
  127. return $this->huoError($_rs, MemberStatus::getMsg($_rs));
  128. }
  129. $_code = MemberStatus::NO_ERROR;
  130. /* 手机绑定手机获得积分 */
  131. if (false == $this->getHasBind()) {
  132. (new MemIa($mem_id))->doItgAct(IaConst::IA_BIND_MOBILE);
  133. }
  134. $_member_count = (new MemberModel()) ->getMemNumber($mobile);
  135. if ($_member_count > 8){
  136. $_code = MemberStatus::PHONE_BIND_TOOMUCH;
  137. return $this->huoError($code, MemberStatus::getMsg($_code));
  138. }
  139. $this->clearSession();
  140. return $this->huoSuccess($_code, MemberStatus::getMsg($_code));
  141. }
  142. /**
  143. * @param int $mem_id
  144. * @param string $mobile
  145. * @param string $password
  146. *
  147. * @return int
  148. */
  149. public function updateData($mem_id, $mobile, $password = '') {
  150. $_mc_class = new MemCache();
  151. if (!empty($password)) {
  152. $_mem_data['password'] = $password;
  153. }
  154. $_mem_data['mobile'] = $mobile;
  155. $_rs = $_mc_class->updateMem($mem_id, $_mem_data);
  156. if (false !== $_rs) {
  157. $_code = MemberStatus::NO_ERROR;
  158. } else {
  159. $_code = MemberStatus::UNKNOWN_ERROR;
  160. }
  161. return $_code;
  162. }
  163. public function clearSession() {
  164. Session::set('bind_mobile.has_bind_ok', 0);
  165. }
  166. /**
  167. * 设置是否校验OK
  168. *
  169. */
  170. public function setCheckOk() {
  171. Session::set('bind_mobile.has_bind_ok', 1);
  172. }
  173. /**
  174. * @return bool
  175. */
  176. public function getCheckOk() {
  177. $_has_bind = Session::get('bind_mobile.has_bind_ok');
  178. if (empty($_has_bind)) {
  179. return false;
  180. }
  181. return true;
  182. }
  183. public function setHasBind($mobile) {
  184. Session::set('bind_mobile.has_bind', 1);
  185. $this->setOldMobile($mobile);
  186. }
  187. public function getHasBind() {
  188. $_has_bind = Session::get('bind_mobile.has_bind');
  189. if (empty($_has_bind)) {
  190. return false;
  191. }
  192. return true;
  193. }
  194. public function setOldMobile($mobile) {
  195. Session::set('bind_mobile.old_mobile', $mobile);
  196. }
  197. public function getOldMobile() {
  198. $_old_mobile = Session::get('bind_mobile.old_mobile');
  199. return $_old_mobile;
  200. }
  201. /**
  202. * 获取绑定信息
  203. *
  204. * @param $mem_id
  205. *
  206. * @return array
  207. */
  208. public function getBindInfo($mem_id) {
  209. $_mc_class = MemCache::ins();
  210. $_mem_data = $_mc_class->getInfoById($mem_id);
  211. if (empty($_mem_data)) {
  212. $_code = MemberStatus::USERNAME_NOT_EXISTS;
  213. return $this->huoError($_code, MemberStatus::getMsg($_code));
  214. }
  215. if (!empty($_mem_data['mobile'])) {
  216. $this->setHasBind($_mem_data['mobile']);
  217. }
  218. $_data = [
  219. 'mobile' => (string)StrUtils::encryptPhone($_mem_data['mobile'], 3, 6),
  220. 'email' => (string)StrUtils::encryptEmail($_mem_data['email']),
  221. ];
  222. return $this->huoSuccess(MemberStatus::NO_ERROR, MemberStatus::getMsg(MemberStatus::NO_ERROR), $_data);
  223. }
  224. }