Transfer.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\main\constants\AdminConstants;
  5. use app\main\service\TransferService;
  6. /**
  7. * 粉丝迁移
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Transfer extends Backend{
  12. /**
  13. * Transfer模型对象
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = model('Transfer');
  20. }
  21. /**
  22. * 添加
  23. */
  24. public function add()
  25. {
  26. if ($this->request->isPost())
  27. {
  28. $params = $this->request->post("row/a");
  29. if ($params)
  30. {
  31. if ($this->dataLimit)
  32. {
  33. $params[$this->dataLimitField] = $this->auth->id;
  34. }
  35. try
  36. {
  37. //是否采用模型验证
  38. if ($this->modelValidate)
  39. {
  40. $name = basename(str_replace('\\', '/', get_class($this->model)));
  41. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  42. $this->model->validate($validate);
  43. }
  44. if(!$this->auth->isSuperAdmin()){
  45. $params['admin_id'] = $this->auth->id;
  46. //检查转移渠道开户人
  47. $this->checkCreateBy($params['transfer_id']);
  48. //检查接收渠道开户人
  49. $this->checkCreateBy($params['receive_id']);
  50. }else{
  51. $params['admin_id'] = 0;
  52. //检查转移渠道
  53. if(!$this->checkChannelOrAgent($params['transfer_id'])){
  54. $this->error("请选择渠道商OR配号代理商[ {$params['transfer_id']} ]");
  55. }
  56. //检查接收渠道
  57. if(!$this->checkChannelOrAgent($params['receive_id'])){
  58. $this->error("请选择渠道商OR配号代理商[ {$params['receive_id']} ]");
  59. }
  60. }
  61. $result = $this->model->allowField(true)->save($params);
  62. if ($result !== false)
  63. {
  64. $this->success();
  65. }
  66. else
  67. {
  68. $this->error($this->model->getError());
  69. }
  70. }
  71. catch (\think\exception\PDOException $e)
  72. {
  73. $this->error($e->getMessage());
  74. }
  75. }
  76. $this->error(__('Parameter %s can not be empty', ''));
  77. }
  78. return $this->view->fetch();
  79. }
  80. public function index(){
  81. //设置过滤方法
  82. $this->request->filter(['strip_tags']);
  83. if ($this->request->isAjax())
  84. {
  85. if(!$this->auth->isSuperAdmin()){
  86. $map['admin_id'] = $this->auth->id;
  87. }else{
  88. $map = null;
  89. }
  90. //如果发送的来源是Selectpage,则转发到Selectpage
  91. if ($this->request->request('pkey_name'))
  92. {
  93. return $this->selectpage();
  94. }
  95. $filter = $this->request->get("filter", '');
  96. $sort = $this->request->get("sort", "id");
  97. $order = $this->request->get("order", "DESC");
  98. $offset = $this->request->get("offset", 0);
  99. $limit = $this->request->get("limit", 0);
  100. $filter = json_decode($filter, TRUE);
  101. $where = [];
  102. if($filter){
  103. foreach ($filter as $k => $v){
  104. if($k == 'transfer_nickname' || $k == 'receive_nickname' || $k == 'id'){
  105. if($k == 'transfer_nickname'){
  106. $where['ad.nickname'] = ['like',"%{$v}%"];
  107. }
  108. if($k == 'receive_nickname'){
  109. $where['ads.nickname'] = ['like',"%{$v}%"];
  110. }
  111. if($k == 'id'){
  112. $where['transfer.id'] = $v;
  113. }
  114. }else{
  115. $where[$k] = $v;
  116. }
  117. }
  118. }
  119. if($sort == 'id'){
  120. $sort = 'transfer.id';
  121. }
  122. $total = $this->model
  123. ->join('admin ad','ad.id = transfer.transfer_id')
  124. ->join('admin ads','ads.id = transfer.receive_id')
  125. ->where($map)
  126. ->where($where)
  127. ->field('transfer.*,ad.nickname as transfer_nickname,ads.nickname as receive_nickname')
  128. ->order($sort, $order)
  129. ->count();
  130. $list = $this->model
  131. ->join('admin ad','ad.id = transfer.transfer_id')
  132. ->join('admin ads','ads.id = transfer.receive_id')
  133. ->where($map)
  134. ->where($where)
  135. ->order($sort, $order)
  136. ->limit($offset, $limit)
  137. ->field('transfer.*,ad.nickname transfer_nickname,ads.nickname receive_nickname')
  138. ->select();
  139. $result = array("total" => $total, "rows" => $list);
  140. return json($result);
  141. }
  142. return $this->fetch();
  143. }
  144. public function backups(){
  145. $next = null;
  146. if(!$id = $this->request->param('ids')){
  147. $this->error('参数错误');
  148. }
  149. if($this->request->has('next')){
  150. $next = $this->request->param('next');
  151. }
  152. if(!$result = $this->model->where('id',$id)->find()){
  153. $this->error('数据获取失败');
  154. }
  155. if(intval($result['transfer_status']) == 2){
  156. $this->error('这条数据已完成了粉丝迁移');
  157. }
  158. $is_update = $this->model->where('id',$id)->update(['backup_status'=>'1','remark'=>'']);
  159. if($is_update !== false ){
  160. TransferService::instance()->setBackups($id,$next)->push();
  161. $this->success('操作成功');
  162. }else{
  163. $this->error('操作失败');
  164. }
  165. }
  166. public function transfer(){
  167. $next = null;
  168. if(!$id = $this->request->param('ids')){
  169. $this->error('参数错误');
  170. }
  171. if(!$result = $this->model->where('id',$id)->find()){
  172. $this->error('数据获取失败');
  173. }
  174. if($this->request->has('next')){
  175. $next = intval($this->request->param('next'));
  176. }
  177. if(intval($result['backup_status']) != 2){
  178. $this->error('请先完成粉丝备份');
  179. }
  180. if(intval($result['transfer_status']) == 2){
  181. $this->error('这条数据已完成了粉丝迁移');
  182. }
  183. $is_update = $this->model->where('id',$id)->update(['transfer_status'=>'1','remark'=>'']);
  184. if($is_update !== false ){
  185. TransferService::instance()->setTransfer($id,$next)->push();
  186. $this->success('操作成功');
  187. }else{
  188. $this->error('操作失败');
  189. }
  190. }
  191. public function checkCreateBy($admin_id){
  192. $create_by = null;
  193. $group_id = model('AuthGroupAccess')->getGroupId($admin_id);
  194. //获取渠道开户人
  195. if(AdminConstants::ADMIN_GROUP_ID_CHANNEL == $group_id){
  196. $create_by = model('AdminExtend')->where('admin_id',$admin_id)->value('create_by');
  197. }else if(AdminConstants::ADMIN_GROUP_ID_AGENT == $group_id){
  198. //检查配号代理商,并检查开户人
  199. $adminExtend = model('AdminExtend')->where('admin_id',$admin_id)->column('distribute,create_by');
  200. if(!$adminExtend){
  201. $this->error("获取代理商[ {$admin_id} ]信息失败");
  202. }
  203. if(!key($adminExtend)){
  204. $this->error("请输入配号代理商的ID,普通代理商[ {$admin_id} ]不能进行迁移");
  205. }
  206. $create_by = model('AdminExtend')->where('admin_id',current($adminExtend))->value('create_by');
  207. }else{
  208. $this->error("请选择渠道商OR配号代理商[ {$admin_id} ]");
  209. }
  210. if(intval($this->auth->id) != intval($create_by)){
  211. $this->error("请选择自己名下的渠道商OR配号代理商[ {$admin_id} ]");
  212. }
  213. }
  214. public function checkChannelOrAgent($admin_id){
  215. $group_id = model('AuthGroupAccess')->getGroupId($admin_id);
  216. if(AdminConstants::ADMIN_GROUP_ID_CHANNEL == $group_id){
  217. return true;
  218. }
  219. if(AdminConstants::ADMIN_GROUP_ID_AGENT == $group_id){
  220. $distribute = model('AdminExtend')->where('admin_id',$admin_id)->value('distribute');
  221. if($distribute){
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. }