Bind.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\agent;
  13. use huo\controller\common\Base;
  14. use huolib\sms\Sms;
  15. use huolib\status\MemberStatus;
  16. use think\Session;
  17. class Bind extends Base {
  18. /**
  19. * 获取已绑定的手机号
  20. *
  21. * @param int $agent_id
  22. *
  23. * @return null
  24. */
  25. public function getHasBindMobile($agent_id) {
  26. $_agent_info = (new AgentCache())->getInfoByAgentId($agent_id);
  27. if (empty($_agent_info) || empty($_agent_info['mobile'])) {
  28. return null;
  29. }
  30. $_mobile = $_agent_info['mobile'];
  31. $this->setHasBind($_mobile);
  32. return $_mobile;
  33. }
  34. /**
  35. * 发送手机短信
  36. *
  37. * @param string $mobile
  38. * @param int $type 4 校验 6 绑定手机
  39. *
  40. * @return array
  41. */
  42. public function smsSend($mobile, $type) {
  43. if ($this->getHasBind()) {
  44. $mobile = $this->getOldMobile();
  45. }
  46. $_rs = (new Sms())->send($mobile, $type);
  47. if (MemberStatus::NO_ERROR == $_rs['code']) {
  48. return $this->huoError($_rs['code'], $_rs['msg'], $_rs['data']);
  49. }
  50. return $this->huoSuccess($_rs['code'], $_rs['msg'], $_rs['data']);
  51. }
  52. /**
  53. * 校验原有手机
  54. *
  55. * @param $mobile
  56. * @param $code
  57. * @param $type
  58. *
  59. * @return array
  60. */
  61. public function checkOldMobile($mobile, $code, $type) {
  62. if ($this->getHasBind()) {
  63. $mobile = $this->getOldMobile();
  64. }
  65. /* 校验短信是否正确 */
  66. $_sms_rs = (new Sms())->check($mobile, $code, $type);
  67. if (MemberStatus::NO_ERROR != $_sms_rs['code']) {
  68. return $this->huoError($_sms_rs['code'], $_sms_rs['msg']);
  69. }
  70. $this->setCheckOk();
  71. $_code = MemberStatus::NO_ERROR;
  72. return $this->huoSuccess($_code, MemberStatus::getMsg($_code));
  73. }
  74. /**
  75. * 绑定手机中更新信息
  76. *
  77. * @param int $agent_id
  78. * @param string $mobile
  79. * @param string $code
  80. * @param int $type
  81. *
  82. * @return array
  83. */
  84. public function bindPost($agent_id, $mobile, $code, $type) {
  85. $_ac_class = AgentCache::ins();
  86. $_agent_data = $_ac_class->getInfoByAgentId($agent_id);
  87. if (empty($mobile)) {
  88. $_code = MemberStatus::PHONE_ERROR;
  89. return $this->huoError($_code, MemberStatus::getMsg($_code));
  90. }
  91. /* 有绑定手机 请先校验原有手机 */
  92. if ($this->getHasBind() && false == $this->getCheckOk()) {
  93. $_code = MemberStatus::PHONE_NOT_CHECK;
  94. return $this->huoError($_code, MemberStatus::getMsg($_code));
  95. }
  96. /* 校验短信是否正确 */
  97. $_sms_rs = (new Sms())->check($mobile, $code, $type);
  98. if (MemberStatus::NO_ERROR != $_sms_rs['code']) {
  99. return $this->huoError($_sms_rs['code'], $_sms_rs['msg']);
  100. }
  101. $_agent_data['mobile'] = $mobile;
  102. $_rs = $_ac_class->updateAgent($agent_id, $_agent_data);
  103. if (MemberStatus::NO_ERROR != $_rs) {
  104. return $this->huoError($_rs, MemberStatus::getMsg($_rs));
  105. }
  106. $_code = MemberStatus::NO_ERROR;
  107. $this->clearSession();
  108. return $this->huoSuccess($_code, MemberStatus::getMsg($_code));
  109. }
  110. /**
  111. *
  112. */
  113. public function clearSession() {
  114. Session::clear('bind_mobile');
  115. }
  116. /**
  117. * 设置是否校验OK
  118. *
  119. */
  120. public function setCheckOk() {
  121. Session::set('bind_mobile.has_bind_ok', 1);
  122. }
  123. /**
  124. * @return bool
  125. */
  126. public function getCheckOk() {
  127. $_has_bind = Session::get('has_bind_ok', 'bind_mobile');
  128. if (empty($_has_bind)) {
  129. return false;
  130. }
  131. return true;
  132. }
  133. public function setHasBind($mobile) {
  134. Session::set('bind_mobile.has_bind', 1);
  135. $this->setOldMobile($mobile);
  136. }
  137. public function getHasBind() {
  138. $_has_bind = Session::get('has_bind', 'bind_mobile');
  139. if (empty($_has_bind)) {
  140. return false;
  141. }
  142. return true;
  143. }
  144. public function setOldMobile($mobile) {
  145. Session::set('bind_mobile.old_mobile', $mobile);
  146. }
  147. public function getOldMobile() {
  148. $_old_mobile = Session::get('old_mobile', 'bind_mobile');
  149. return $_old_mobile;
  150. }
  151. }