MemoauthModel.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /**
  3. * MemoauthModel.php UTF-8
  4. *
  5. *
  6. * @date : 2018/4/25 23:06
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\member;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use huolib\constant\CommonConst;
  16. use huolib\constant\MemConst;
  17. use huolib\constant\MpConfConst;
  18. use huolib\constant\OauthConst;
  19. use huomp\model\weixin\MpConfModel;
  20. use think\Cache;
  21. class MemoauthModel extends CommonModel {
  22. protected $name = 'mem_oauth';
  23. //设置只读字段
  24. //protected $readonly = ['from', 'openid', 'create_time'];
  25. protected $readonly = ['from', 'create_time']; //重置玩家需要修改openid chenbingling 20181026
  26. // 开启自动写入时间戳字段
  27. protected $autoWriteTimestamp = true;
  28. protected $cache_key_prefix = CacheConst::CACHE_MEM_OAUTH_MEM_PREFIX;
  29. /**
  30. * @param array $data
  31. *
  32. * @return bool|mixed
  33. */
  34. public function addOauth($data = []) {
  35. $_data['from'] = get_val($data, 'from', '');
  36. $_data['conf_id'] = get_val($data, 'conf_id', '0');
  37. $_data['openid'] = get_val($data, 'openid', '');
  38. $_data['access_token'] = get_val($data, 'access_token', '');
  39. $_data['unionid'] = get_val($data, 'unionid', '');
  40. $_data['nickname'] = get_val($data, 'nickname', '');
  41. $_data['gender'] = get_val($data, 'gender', MemConst::GENDER_N);
  42. $_data['avatar'] = get_val($data, 'avatar', '');
  43. $_data['mem_id'] = get_val($data, 'mem_id', 0);
  44. $_data['last_login_time'] = time();
  45. $_data['last_login_ip'] = get_client_ip(0, true);
  46. $_data['status'] = get_val($data, 'status', MemConst::STATUS_NORMAL);
  47. $_data['expires_in'] = get_val($data, 'expires_in', 0);
  48. $_data['more'] = get_val($data, 'more', '');
  49. if ($_obj = self::create($_data, true)) {
  50. return $_obj->id;
  51. } else {
  52. return false;
  53. }
  54. }
  55. /**
  56. * 更新Oauth信息
  57. *
  58. * @param $from
  59. * @param $open_id
  60. * @param $oauth_data
  61. *
  62. * @return bool
  63. */
  64. public function updateOauth($from, $open_id, $oauth_data) {
  65. if (empty($from) || empty($open_id)) {
  66. return false;
  67. }
  68. $_oauth_data = $this->getInfoByOpenId($from, $open_id);
  69. $_map['from'] = $from;
  70. $_map['openid'] = $open_id;
  71. $_rs = self::update($oauth_data, $_map, true);
  72. if (false === $_rs) {
  73. return false;
  74. }
  75. $_oauth_data = array_merge($_oauth_data, $oauth_data);
  76. $_cache_conf_key = $this->cache_key_prefix.$_oauth_data['conf_id'].$_oauth_data['mem_id'];
  77. Cache::set($_cache_conf_key, $_oauth_data, CommonConst::CONST_DAY_SECONDS);
  78. $_cache_key = $this->cache_key_prefix.$open_id.$from;
  79. Cache::set($_cache_key, $_oauth_data, CommonConst::CONST_DAY_SECONDS);
  80. return true;
  81. }
  82. /**
  83. * 通过配置ID与玩家ID获取数据
  84. *
  85. * @param int $conf_id 配置ID
  86. * @param int $mem_id 玩家ID
  87. *
  88. * @return array|bool|
  89. */
  90. public function getDataByMpMemId($conf_id, $mem_id) {
  91. if (empty($conf_id) || empty($mem_id)) {
  92. return false;
  93. }
  94. $_cache_key = $this->cache_key_prefix.$conf_id.$mem_id;
  95. $_data = Cache::get($_cache_key);
  96. if (!empty($_data)) {
  97. return $_data;
  98. }
  99. $_map['conf_id'] = $conf_id;
  100. $_map['mem_id'] = $mem_id;
  101. $_data = $this->where($_map)->find();
  102. if (false == $_data) {
  103. return false;
  104. }
  105. if (is_object($_data)) {
  106. $_data = $_data->toArray();
  107. }
  108. if (!empty($_data)) {
  109. Cache::set($_cache_key, $_data, CommonConst::CONST_DAY_SECONDS);
  110. }
  111. return $_data;
  112. }
  113. /**
  114. * 通过配置ID与玩家ID获取Open_id
  115. *
  116. * @param int $conf_id 配置ID
  117. * @param int $mem_id 玩家ID
  118. *
  119. * @return bool|mixed
  120. */
  121. public function getOpenIdByMpMemId($conf_id, $mem_id) {
  122. $_data = $this->getDataByMpMemId($conf_id, $mem_id);
  123. if (empty($_data)) {
  124. return false;
  125. }
  126. return $_data['openid'];
  127. }
  128. /**
  129. * 通过open_id 获取信息
  130. *
  131. * @param string $type
  132. * @param string $open_id
  133. *
  134. * @return array|false
  135. */
  136. public function getInfoByOpenId($type, $open_id) {
  137. $_cache_key = $this->cache_key_prefix.$open_id.$type;
  138. $_data = Cache::get($_cache_key);
  139. if (!empty($_data)) {
  140. return $_data;
  141. }
  142. $_map['from'] = $type;
  143. $_map['openid'] = $open_id;
  144. $_data = $this->where($_map)->find();
  145. if (is_object($_data)) {
  146. $_data = $_data->toArray();
  147. }
  148. if (empty($_data)) {
  149. return false;
  150. }
  151. if (!empty($_data)) {
  152. Cache::set($_cache_key, $_data, CommonConst::CONST_DAY_SECONDS);
  153. }
  154. return $_data;
  155. }
  156. /**
  157. * @param $type
  158. * @param $open_id
  159. *
  160. * @return bool|int|mixed
  161. */
  162. public function getMemIdByOpenId($type, $open_id) {
  163. $_data = $this->getInfoByOpenId($type, $open_id);
  164. if (empty($_data)) {
  165. return 0;
  166. }
  167. return $_data['mem_id'];
  168. }
  169. /**
  170. * @param $type
  171. * @param $union_id
  172. *
  173. * @return bool|int|mixed
  174. */
  175. public function getMemIdByUnionId($type, $union_id) {
  176. $_wx_type_array = [OauthConst::OAUTH_MP, OauthConst::OAUTH_WEIXIN, OauthConst::OAUTH_WXQRCODE];
  177. if (in_array($type, $_wx_type_array)) {
  178. $_map['from'] = ['in', $_wx_type_array];
  179. } else {
  180. $_map['from'] = $type;
  181. }
  182. $_map['unionid'] = $union_id;
  183. $_map['mem_id'] = ['gt', 0];
  184. $_mem_id = $this->where($_map)->value('mem_id');
  185. if (false === $_mem_id) {
  186. return false;
  187. }
  188. if (empty($_mem_id)) {
  189. return 0;
  190. }
  191. return $_mem_id;
  192. }
  193. /**
  194. * @param $type
  195. * @param $mem_id
  196. *
  197. * @return bool|int|mixed
  198. */
  199. public function getOpenidByMemId($type, $mem_id, $conf_id = 0) {
  200. if (!empty($type)) {
  201. $_map['from'] = $type;
  202. }
  203. if (!empty($conf_id)) {
  204. $_map['conf_id'] = $conf_id;
  205. }
  206. $_map['mem_id'] = $mem_id;
  207. $_open_id = $this->where($_map)->value('openid');
  208. if (false === $_open_id) {
  209. return false;
  210. }
  211. if (empty($_open_id)) {
  212. return 0;
  213. }
  214. return $_open_id;
  215. }
  216. /**
  217. * 获取第三方数据
  218. *
  219. * @param $conf_id
  220. * @param $mem_id
  221. *
  222. * @return array|bool|false
  223. */
  224. public function getInfoByConfId($conf_id, $mem_id) {
  225. $_map['conf_id'] = $conf_id;
  226. $_map['mem_id'] = $mem_id;
  227. $_data = $this->where($_map)->find();
  228. if (is_object($_data)) {
  229. $_data = $_data->toArray();
  230. }
  231. if (empty($_data)) {
  232. return false;
  233. }
  234. return $_data;
  235. }
  236. /**
  237. * 根据应用于玩家ID获取信息
  238. *
  239. * @param $app_id
  240. * @param $mem_id
  241. *
  242. * @return array|bool
  243. */
  244. public function getInfoByAppMemId($app_id, $mem_id) {
  245. $_type = MpConfConst::MP_CONF_TYPE_6;
  246. $_conf_id = (new MpConfModel())->getIdByAppId($app_id, $_type);
  247. if (empty($_conf_id)) {
  248. return [];
  249. }
  250. return $this->getDataByMpMemId($_conf_id, $mem_id);
  251. }
  252. /**
  253. * 删除缓存
  254. *
  255. * @param $conf_id
  256. * @param $open_id
  257. * @param $mem_id
  258. * @param $from
  259. */
  260. public function clearCache($conf_id, $open_id, $mem_id, $from) {
  261. $_cache_conf_key = $this->cache_key_prefix.$conf_id.$mem_id;
  262. Cache::rm($_cache_conf_key);
  263. $_cache_key = $this->cache_key_prefix.$open_id.$from;
  264. Cache::rm($_cache_key);
  265. $_key = CacheConst::CACHE_MEM_OAUTH_OPENID_MEM_PREFIX.$from.'_'.$open_id;
  266. Cache::rm($_key);
  267. }
  268. /**
  269. * 根据微信ID与玩家ID获取信息
  270. *
  271. * @param $mp_id
  272. * @param $mem_id
  273. *
  274. * @return array|bool
  275. */
  276. public function getInfoByMpMemId($mp_id, $mem_id) {
  277. $_conf_id = (new MpConfModel())->getIdByMpId($mp_id);
  278. if (empty($_conf_id)) {
  279. return [];
  280. }
  281. return $this->getDataByMpMemId($_conf_id, $mem_id);
  282. }
  283. /**
  284. * @param $type
  285. * @param $mem_id
  286. *
  287. * @return bool|int|mixed
  288. */
  289. public function getConfidByMemId($type, $mem_id) {
  290. if (!empty($type)) {
  291. $_map['from'] = $type;
  292. }
  293. $_map['mem_id'] = $mem_id;
  294. $_conf_id = $this->where($_map)->value('conf_id');
  295. if (false === $_conf_id) {
  296. return false;
  297. }
  298. if (empty($_conf_id)) {
  299. return 0;
  300. }
  301. return $_conf_id;
  302. }
  303. /**
  304. * @param $type
  305. * @param $mem_id
  306. *
  307. * @return bool|int|mixed
  308. */
  309. public function getTypeByMemId($mem_id) {
  310. $_map['mem_id'] = $mem_id;
  311. $_from = $this->where($_map)->value('from');
  312. if (false === $_from) {
  313. return false;
  314. }
  315. if (empty($_from)) {
  316. return 0;
  317. }
  318. return $_from;
  319. }
  320. /**
  321. * 左联玩家表
  322. */
  323. public function ljmem() {
  324. return $this->belongsTo('huo\model\member\MemberModel', 'mem_id', 'id', [], 'left')->setEagerlyType(0);
  325. }
  326. }